no

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;

            try
            {
                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());
                writer.WriteLine(InitializeXml());
                writer.Close();                
    
    //read the response
                webResponse = webRequest.GetResponse() as HttpWebResponse;    
                var reader = new StreamReader(webResponse.GetResponseStream());
                var reply = reader.ReadToEnd();
                reader.Close();
    
    //load the reply in an xml node
                var xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(reply);
            }
            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();
                reader.Close();
                reader.Dispose();
                var xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(xmlData);
    
    //you can do something with the xmlData here
    
    //Response
                Response.ContentType = "text/xml";
                Response.ContentEncoding = Encoding.UTF8;
                var writer = new StreamWriter(Response.OutputStream);
                writer.Write(xmlTextHere);
                writer.Flush();
                writer.Close();
                writer.Dispose();


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.
            dataStream.Close();
            // Get the response.
            var response = request.GetResponse();
            // Display the status.
            //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // 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.
            //Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            if (dataStream != null) dataStream.Close();
            response.Close();

            responseFromServer = "";

            return responseFromServer;
        }

Related

javaee 5179954783954832071

Post a Comment Default Comments

item