3

I want to post an xml document to an asp page from an asp.net page. If I use WebRequest with content/type text/xml the document never gets to the asp page. How can I do this ?

3
  • Are you using .NET 2.0? Do you see any errors? Commented Oct 11, 2008 at 19:18
  • You might be best served adding some code to the question. Commented Oct 12, 2008 at 21:11
  • yes i am using .NET 2.0 no error. but i am not getting any reply content from that. but when i do same with PHP i am getting response content from that. Commented Dec 28, 2009 at 13:07

3 Answers 3

2

Here is a sample without any error handling (do it yourself :) ):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData);
byte[] byteStream;
byteStream = System.Text.Encoding.UTF8.GetBytes(sendString);

request.Method = POST;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteStream.LongLength;

using(Stream writer = request.GetRequestStream())
{
    writer.Write(byteStream, 0, (int)request.ContentLength);
    writer.Flush();
}

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

//read the response
Sign up to request clarification or add additional context in comments.

1 Comment

i am implementing it in this way : string targetUri = "hostelspoint.com/xml/xml.php"; System.Xml.XmlDocument reqDoc = new System.Xml.XmlDocument(); reqDoc.Load(Server.MapPath("~\\myfile.xml")); string xmlData = reqDoc.InnerXml; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri); string sendString = formParameterName + "=" + HttpUtility.UrlEncode(xmlData); what will "formParameterName" contain?
0

It's absolutely possible. Make sure that you are writing the XML to the RequestStream.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getrequeststream.aspx

Comments

0

I do use GetRequestStream. But if you try to send xml like <data id='10'>value</data> with content-type text/xml the document never gets to its destination

1 Comment

It is difficult to know how you are determining that the document never makes it to the destination. Does the code in the ASP actually run? How are you consuming the Request entity? Show us some code! Please don't post another answer this is not an NG or forum. Edit your original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.