Solved

C# and XML Web Request

Posted on 2011-03-09
5
817 Views
Last Modified: 2013-12-17
Hi Experts,
As you can see by the code below, I haave no clue what I'm doing.  I need to post an XML string as a web request to a eCheck processor web site and read the reply.  The portion of code below is my request to the site.  I don't know if I have everything in the order it needs to be and if I have code that isn't needed in the request.  When I run the code, it appears that the request is sent (I receive no errors), but when I check the web site to see if the post was successful, there is nothing there.  Please help.


        private void btnSubmit_Click(object sender, EventArgs e)

        {
           
            string hPhone="";        
            string addrs1="";          
            string city="";
            string state="";
            string zip="";
            string v_strURL;
            XmlDocument v_objXMLDoc = new XmlDocument();
            StringWriter st = new StringWriter();

            v_strURL = "https://ics2wstest.ic3.com";

            SqlConnection con = new SqlConnection();
            con.ConnectionString = Constants.TBG_ConString;

            SqlCommand cmdSendRequest = new SqlCommand("SELECT rtrim(PHONE1) AS PHONE1,"
            + "rtrim(ADDRESS1) AS ADDRESS1,rtrim(CITY) AS CITY,rtrim(STATE) AS STATE,"
            + "rtrim(ZIP) AS ZIP FROM RM00101"
            + " WHERE CUSTNMBR ='" + txtCustNum.Text + "'", con);

            cmdSendRequest.CommandType = CommandType.Text;

            con.Open();
         
            SqlDataReader reader = cmdSendRequest.ExecuteReader();

            if (reader.Read()) // or while
            {
                hPhone = reader["PHONE1"].ToString();
                addrs1 = reader["ADDRESS1"].ToString();
                city = reader["CITY"].ToString();
                state = reader["STATE"].ToString();
                zip = reader["ZIP"].ToString();
            }

            con.Close();

            XmlTextWriter xWriter = new XmlTextWriter(st);

            xWriter.WriteStartDocument();

            xWriter.WriteStartElement("requestMessage");
            xWriter.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
            xWriter.WriteAttributeString("xmlns:tns", "urn:schemas-cybersource-com:transaction-data-1.51");
            xWriter.WriteAttributeString("targetNamespace", "urn:schemas-cybersource-com:transaction-data-1.51");
            xWriter.WriteAttributeString("elementFormDefault", "qualified");
            xWriter.WriteAttributeString("attributeFormDefault", "unqualified");
            xWriter.WriteElementString("merchantID", "wfgbellgroup");
            xWriter.WriteElementString("merchantReferenceCode", txtCustNum.Text);
            xWriter.WriteStartElement("billTo");
            xWriter.WriteAttributeString("firstName", txtFirstName.Text);
            xWriter.WriteAttributeString("lastName", txtLastName.Text);
            xWriter.WriteAttributeString("street1", addrs1);
            xWriter.WriteAttributeString("city", city);
            xWriter.WriteAttributeString("state", state);
            xWriter.WriteAttributeString("postalCode", zip);
            xWriter.WriteAttributeString("country", "US");
            xWriter.WriteAttributeString("phoneNumber", hPhone);
            xWriter.WriteAttributeString("email", "jdoe@example.com");
            xWriter.WriteEndElement();
            xWriter.WriteStartElement("purchaseTotals");
            xWriter.WriteAttributeString("purchaseTotals_currency", "USD");
            xWriter.WriteAttributeString("purchaseTotals_grandTotalAmount", txtCkAmnt.Text);
            xWriter.WriteEndElement();
            xWriter.WriteStartElement("check");
            xWriter.WriteAttributeString("accountNumber", txtCkActNum.Text);
            xWriter.WriteAttributeString("accountType", "C");
            xWriter.WriteAttributeString("bankTransitNumber", txtRoutNum.Text);
            xWriter.WriteEndElement();
            xWriter.WriteElementString("ecDebitService", "run = true");

            string requestMessageXml = st.ToString().Replace("utf-16", "UTF-8"); ;

            xWriter.WriteEndDocument();
            xWriter.Close();

            //Declare XMLResponse document
            XmlDocument replyMessage = null;

            //Declare an HTTP-specific implementation of the WebRequest class.
            HttpWebRequest objHttpWebRequest;

            //Declare an HTTP-specific implementation of the WebResponse class
            HttpWebResponse objHttpWebResponse = null;

            //Declare a generic view of a sequence of bytes
            Stream objRequestStream = null;
            Stream objResponseStream = null;

            //Declare XMLReader
            XmlTextReader objXMLReader;

            //Creates an HttpWebRequest for the specified URL.
            objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);


            //---------- Start HttpRequest

            //Set HttpWebRequest properties
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.InnerXml);


            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            objHttpWebRequest.Method = "POST";

            objHttpWebRequest.ContentLength = bytes.Length;
            objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

            //Get Stream object

            objRequestStream = objHttpWebRequest.GetRequestStream();

            //---------- End HttpRequest  
}
0
Comment
Question by:isda-don
  • 3
  • 2
5 Comments
 

Author Comment

by:isda-don
Comment Utility
remainder of code for btnSubmit_Click event.

 //---------- End HttpRequest  


            //Start replyMessage XML

                XmlTextReader DebitReply = new XmlTextReader("replyMessage.xml");
                try
                {
                    while (DebitReply.Read())
                    {
                        switch (DebitReply.NodeType)
                        {
                            case XmlNodeType.Element: // The node is an element.
                                Console.Write("<C:" + DebitReply.Name);

                                while (DebitReply.MoveToNextAttribute()) // Read the attributes.
                                    Console.Write(" " + DebitReply.Name + "='" + DebitReply.Value + "'");

                                Console.WriteLine(">");

                                break;

                            case XmlNodeType.Text: //Display the text in each element.
                                Console.WriteLine(DebitReply.Value);

                                break;

                            case XmlNodeType.EndElement: //Display the end of the element.
                                Console.Write("</C:" + DebitReply.Name);

                                Console.WriteLine(">");

                                break;
                        }

                        Console.WriteLine(DebitReply.Name);
                    }

                    Console.ReadLine();
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            //End replyMessage XML



        }
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
Comment Utility
Hi,

I see there is no code written to write the xml bytes into objRequestStream which will actually post the data to the server:

 //Get Stream object
// objRequestStream = objHttpWebRequest.GetRequestStream();  Not required
 //---------- End HttpRequest  
// Write the request data to the server.
using (Stream objRequestStream = objHttpWebRequest .GetRequestStream())
{
 objRequestStream .Write(bytes, 0, bytes.Length);
}
0
 

Author Comment

by:isda-don
Comment Utility
Thanks for the response Expert, but the request is still not posting to the server.
0
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 500 total points
Comment Utility
Hi isda,
Sorry i didn't see this one in my earlier scan.
The below line will be of no use as v_objXMLDoc object is not used in request data.
<<bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.InnerXml);
Replace this statement which has request data in the string "requestMessageXml"
bytes = System.Text.Encoding.ASCII.GetBytes(requestMessageXml);

Hope this will work!
0
 

Author Closing Comment

by:isda-don
Comment Utility
Thanks very much.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now