Posting XML to a Url with C# and getting the response

A few friends in the past have asked me about posting XML to a url, using ASP.NET. This is usually on e-commerce sites, where details need to be posted to a secure site to process credit card details, etc.

Here’s a simple C# function which can post an XML document to a url and get the response. Can easily be modified to post dynamically generated XML (as you’ll probably want to do).

private string PostXmlAndGetResponse(string xmlfile, string url)
{
 WebRequest webRequest = null;
 WebResponse webResponse = null;
 string returnVal = string.Empty;
 try
 {
  webRequest = WebRequest.Create(url);
  webRequest.Method = "POST";
  webRequest.ContentType = "text/xml";
  string fileContents = string.Empty;
  using (StreamReader sr = new StreamReader(xmlfile))
  {
   fileContents = sr.ReadToEnd();
   sr.Close();
  }
  using (StreamWriter sw = new StreamWriter(webRequest.GetRequestStream()))
  {
   sw.WriteLine(fileContents);
  }
  webResponse = webRequest.GetResponse();
  using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
  {
   returnVal = sr.ReadToEnd();
   sr.Close();
  }
 }
 catch (Exception ex)
 {
  returnVal = string.Format("Error: {0}", ex.ToString());
 }
 finally
 {
  if (webRequest != null)
   webRequest.GetRequestStream().Close();
  if (webResponse != null)
   webResponse.GetResponseStream().Close();
 }
 return (returnVal);
}

Hope this saves some other developers time :-)

This entry was posted in Uncategorized by Mun. Bookmark the permalink.

2 thoughts on “Posting XML to a Url with C# and getting the response

  1. doesnot work show blank space my code is

    WebRequest webRequest = null;
    WebResponse webResponse = null;
    string uri = “http://59.162.33.102:9301/Avalability”;
    string xmlInternationalFlight = File.ReadAllText(file);
    string returnVal = string.Empty;
    try
    {
    webRequest = WebRequest.Create(uri);
    webRequest.Method = “POST”;
    webRequest.ContentType = “text/xml”;
    string fileContents = string.Empty;
    using (StreamReader sr = new StreamReader(file))
    {
    fileContents = sr.ReadToEnd();
    sr.Close();
    }
    using (StreamWriter sw1 = new StreamWriter(webRequest.GetRequestStream()))
    {
    sw1.WriteLine(fileContents);
    }
    webResponse = webRequest.GetResponse();
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
    returnVal = sr.ReadToEnd();
    sr.Close();
    }
    }

    plz reply me at my mail in case u have time plz i needed this

  2. Hello, thank you very much for posting this example. I am new to using XML and wondering if you could help me with an XML issue in C#. I have code working to send my login XML string to a remote API, but need to figure out how to send another XML string during the same connection to the API (basically XML comands to the API after successful login response returned from API) .

    Regards,

    Will R.

Leave a Reply

Your email address will not be published. Required fields are marked *

*