XML Request Response in C#

The following code example performs the following

  • Opens connection to a webservice URL
  • Passes through XML request
  • Reads XML response from webservice

Note this example relies on a Literal ‘ltlOut’ being present to spit the response into.

I have added it here so I can find this example again if required


    // Build XML Request String
    StringWriter xmlString = new StringWriter();
    XmlTextWriter writer = new XmlTextWriter(xmlString);

    // writer.WriteStartDocument(); // Removed to force UTF8
    writer.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"");
    
    writer.WriteStartElement("API");
    //    writer.WriteAttributeString("thisAtt", "It's Value"); // example of setting an attribute
    writer.WriteElementString("userid", _Username);
    writer.WriteElementString("password", _Password);
    writer.WriteElementString("action", "services");
    writer.WriteEndElement();
    
    writer.Flush();
    writer.Close();

    ErrorLog.LogError("XmlTest", "Services Request", xmlString.ToString(),true);

    // Send XML Request String
    var encoding = new System.Text.ASCIIEncoding();
    byte[] reqBytes = encoding.GetBytes(xmlString.ToString());

    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_WebserviceUrl);
    httpRequest.Method = "POST";
    httpRequest.ContentType = "application/xml";
    httpRequest.ContentLength = reqBytes.Length;
    Stream requestStream = httpRequest.GetRequestStream();
    requestStream.Write(reqBytes, 0, reqBytes.Length);

    // Retrieve XML Result
    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
    StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);
    string xmlResponse = streamReader.ReadToEnd();

    // Log Response
    ErrorLog.LogError("XmlTest", "Services Response", xmlResponse, true);

    streamReader.Close();
    httpResponse.Close();

There is an alternate method where you can write the XML straight to the stream however I generally like to store it in a string for debugging and logging purposes


    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_WebserviceUrl);
    httpRequest.Method = "POST";
    httpRequest.ContentType = "application/xml";
    Stream requestStream = httpRequest.GetRequestStream();

    XmlTextWriter writer = new XmlTextWriter(requestStream, System.Text.Encoding.UTF8);

    writer.WriteStartDocument();
    writer.WriteStartElement("API");
    //    writer.WriteAttributeString("thisAtt", "It's Value");
    writer.WriteElementString("userid", _Username);
    writer.WriteElementString("password", _Password);
    writer.WriteElementString("action", "request");

    writer.WriteStartElement("request");

    writer.WriteStartElement("imei");
    writer.WriteElementString("service", productCode);
    writer.WriteElementString("imei", imei);
    writer.WriteEndElement();

    writer.WriteEndElement();

    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();

    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
    StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);

    // Your response here
    string xmlResponse = streamReader.ReadToEnd());

    streamReader.Close();
    httpResponse.Close();

Tags: , , , , ,

Leave a Reply