Sending and Receiving Xml Through Http Post

Usually, it's easier done on dotnet webservice but there are times when your client wants to do this using Http Post. So I've crea...

Usually, it's easier done on dotnet webservice but there are times when your client wants to do this using Http Post.

So I've created 2 test pages, one that will request and with for a response while the other will accept the request and return a response.

1.) Request Page (Request.aspx)
WebRequest webRequest = null;
            HttpWebResponse webResponse = null;

                string uri = Request.Url.ToString().Substring(0, Request.Url.ToString().LastIndexOf("/")) + "/Post.aspx";
                webRequest = WebRequest.Create(txtLink.Text);
    //webRequest.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
                webRequest.Method = "POST";        // Post method
                webRequest.ContentType = "text/xml";     // content type
    //Wrap the request stream with a text-based writer
                var writer = new StreamWriter(webRequest.GetRequestStream());
    //read the response
                webResponse = webRequest.GetResponse() as HttpWebResponse;    
                var reader = new StreamReader(webResponse.GetResponseStream());
                var reply = reader.ReadToEnd();
    //load the reply in an xml node
                var xmlDocument = new XmlDocument();
            catch (WebException we)


2.) Accepts the request (Post.aspx.cs)
Note: It is very important to clean the content of the Post.aspx page, all the html tags except <%@ Page Language=...
So, Post.aspx has a single line of code and everything is in Post.aspx.cs
if (!IsPostBack)
                // Read XML posted via HTTP
                var reader = new StreamReader(Request.InputStream);
                var xmlData = reader.ReadToEnd();
                var xmlDocument = new XmlDocument();
    //you can do something with the xmlData here
                Response.ContentType = "text/xml";
                Response.ContentEncoding = Encoding.UTF8;
                var writer = new StreamWriter(Response.OutputStream);

Here's how to return a string:
public static string HttpPost(String url)
            // Create a request using a URL that can receive a post. 
            var request = WebRequest.Create(url);
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            //const string postData = "This is a test that posts this string to a Web server.";
            //var byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            //request.ContentLength = byteArray.Length;
            // Get the request stream.
            var dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            //dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            // Get the response.
            var response = request.GetResponse();
            // Display the status.
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            var reader = new StreamReader(dataStream);
            // Read the content.
            var responseFromServer = reader.ReadToEnd();
            // Display the content.
            // Clean up the streams.
            if (dataStream != null) dataStream.Close();

            responseFromServer = "";

            return responseFromServer;


javaee 5179954783954832071

Post a Comment Default Comments