Aishwarya D

Aishwarya D

  • NA
  • 105
  • 6.7k

I want to post xml data in asp.net

Dec 15 2015 3:07 AM
Hi All,
 
I have created 2 webforms named as requester and responder. Here I want to some XML data in requester webform.  Here is the coding for requester:
 
protected void btnPostXml_Click(object sender, EventArgs e)
{
System.Net.WebRequest req = null;
System.Net.WebResponse rsp = null;
try
{
string uri = txtURI.Text;
req = System.Net.WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";
System.IO.StreamWriter writer = new System.IO.StreamWriter(req.GetRequestStream());
writer.WriteLine(txtXMLData.Text);
writer.Close();
rsp = req.GetResponse();
//Response.Redirect("Responder.aspx");
}
catch
{
throw;
}
finally
{
if (req != null) req.GetRequestStream().Close();
if (rsp != null) rsp.GetResponseStream().Close();
}
}
 Responder:
Page.Response.ContentType = "text/xml";
System.IO.StreamReader reader = new System.IO.StreamReader(Page.Request.InputStream);
String xmlData = reader.ReadToEnd();
System.IO.StreamWriter SW;
SW = File.CreateText(Server.MapPath(".") + @"\" + Guid.NewGuid() + ".txt");
SW.WriteLine(xmlData);
SW.Close();
 
While Executing this program. It will throw an exception. Can any one help me with this coding.
 
 

Answers (2)