C# and XML Web Request

Posted on 2011-03-09
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 = "";

            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;

            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();


            XmlTextWriter xWriter = new XmlTextWriter(st);


            xWriter.WriteAttributeString("xmlns:xsd", "");
            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.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", "");
            xWriter.WriteAttributeString("purchaseTotals_currency", "USD");
            xWriter.WriteAttributeString("purchaseTotals_grandTotalAmount", txtCkAmnt.Text);
            xWriter.WriteAttributeString("accountNumber", txtCkActNum.Text);
            xWriter.WriteAttributeString("accountType", "C");
            xWriter.WriteAttributeString("bankTransitNumber", txtRoutNum.Text);
            xWriter.WriteElementString("ecDebitService", "run = true");

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


            //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  
Question by:isda-don
  • 3
  • 2

Author Comment

ID: 35085896
remainder of code for btnSubmit_Click event.

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

            //Start replyMessage XML

                XmlTextReader DebitReply = new XmlTextReader("replyMessage.xml");
                    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 + "'");



                            case XmlNodeType.Text: //Display the text in each element.


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





                catch (Exception ex)

            //End replyMessage XML

LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 35093340

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);

Author Comment

ID: 35111058
Thanks for the response Expert, but the request is still not posting to the server.
LVL 11

Accepted Solution

Sudhakar Pulivarthi earned 500 total points
ID: 35114016
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!

Author Closing Comment

ID: 35139355
Thanks very much.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

16 Experts available now in Live!

Get 1:1 Help Now