Solved

C# and XML Web Request

Posted on 2011-03-09
5
836 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:isda-don
ID: 35085896
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
ID: 35093340
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
ID: 35111058
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
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!
0
 

Author Closing Comment

by:isda-don
ID: 35139355
Thanks very much.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

733 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