XMLHttpRequest Equivalent in C#

Posted on 2011-10-17
Last Modified: 2012-05-12
I have to create a simple winform which will take in the URL and the input request XML as a parameter and once the user hits the Submit button, the response has to be submitted in a text box. I implemented the same in a .html file and the sequence of operations after the Submit button is hit is shown below:

document.forms["testForm"].resultXML.value = 'Please Wait....'

              var xmlhttp = new XMLHttpRequest()

  "POST", document.forms["testForm"].uri.value,true);

            xmlhttp.setRequestHeader("Man", "POST " + document.forms["testForm"].uri.value + "HTTP/1.1")

            xmlhttp.setRequestHeader("MessageType", "CALL")

             xmlhttp.setRequestHeader("Content-Type", "text/xml")


             xmlhttp.onreadystatechange = function() {

                   document.forms["testForm"].resultXML.value = xmlhttp.responseText



Sample code in C# will be helpful to do the above operations.
Question by:supreeths84

    Accepted Solution

    I got the answer myself:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(requestXML);
                request.Method = "POST";
                request.ContentType = "text/xml;charset=utf-8";
                request.ContentLength = requestBytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
                string responseXML = sr.ReadToEnd();

    Author Closing Comment

    The response time was slow and I could get the answer myself after a bit of searching on the internet

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now