C# and XML Web Request

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  
}
isda-donAsked:
Who is Participating?
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
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
 
isda-donAuthor Commented:
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
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
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
 
isda-donAuthor Commented:
Thanks for the response Expert, but the request is still not posting to the server.
0
 
isda-donAuthor Commented:
Thanks very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.