?
Solved

C# and XML Web Request

Posted on 2011-03-09
5
Medium Priority
?
861 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
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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses

850 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