Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2491
  • Last Modified:

XMLHttpRequest Equivalent in C#

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()

            xmlhttp.open("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.send(document.forms["testForm"].requestXML.value)

             xmlhttp.onreadystatechange = function() {

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

                   }

            }

Sample code in C# will be helpful to do the above operations.
 
0
supreeths84
Asked:
supreeths84
  • 2
1 Solution
 
supreeths84Author Commented:
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);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
            string responseXML = sr.ReadToEnd();
0
 
supreeths84Author Commented:
The response time was slow and I could get the answer myself after a bit of searching on the internet
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now