C # to POST HTTP with XML

Source: Internet
Author: User
Tags xslt

HttpWebRequest request = null;
WebResponse response = null;

Try
{

String myurl = "https://blah.blah.gov/themagicform.cfm"; The URL for my requests:
Request = (HttpWebRequest) webrequest.create (Myurl); Create a request using a URL that can receive a post.
Request. Credentials = new NetworkCredential ("MyUserName", "MyPassword"); The credentials for the website.
Request.  method = "POST"; The Set the method property is the request to POST.
Request.   ContentType = "Text/xml"; Set the ContentType property of the WebRequest.
Request. ContentType = "application/x-www-form-urlencoded";
Specify Headers
Request. Headers.add ("Http_host", "MyZone");
Request. Headers.add ("Http_user_agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.2. NET CLR 1.1.4322);
Request. Headers.add ("Http_accept", "Text/xml");

StreamWriter writer = new StreamWriter (request. GetRequestStream ()); Wrap the request stream with a text-based writer

Writer.  WriteLine (xmldoc); Write the XML text into the stream
Writer. Close ();

/* Previously I tried:
string postdata = xmldoc; Create POST data and convert it to a byte array.
byte[] ByteArray = Encoding.UTF8.GetBytes (postdata);
Request.   ContentLength = Bytearray.length; Set the ContentLength property of the WebRequest.
Stream DataStream = Request. GetRequestStream (); Get the request stream.
Datastream.write (ByteArray, 0, bytearray.length); Write the data to the request stream.
Datastream.close (); Close the Stream Object-which submits the "form" data.
*/

Stream dataStream = null;

Send the data to the webserver
Response = Request. GetResponse (); Get the response.
Lbl_responseinfo.text = ((httpwebresponse) response). Statusdescription.tostring (); Display the status.

DataStream = Response. GetResponseStream (); Get the stream containing content returned by the server.
StreamReader Incomingstreamreader = new StreamReader (dataStream); The Open the stream using a StreamReader for easy access.
String responsefromserver = Incomingstreamreader.readtoend ()//Read the content.
Display the content. Xmlresponse is a special type of label on my ASP page
Xmlresponse.visible = true;
Xmlresponse.transformsource = "Defaultss.xslt";
Xmlresponse.documentcontent = Responsefromserver;
Lbl_received.visible = true;

Clean up the streams.
Incomingstreamreader.close ();
Datastream.close ();
Response. Close ();

Display the XML we sent
Xmlsent.visible = true;
Xmlsent.transformsource = "Defaultss.xslt";
Xmlsent.documentcontent = xmldoc;
Lbl_sent.visible = true;
}

catch (WebException webEx)
{
Lbl_responseinfo.text = WebEx.Message.ToString ();
}
catch (Exception ex)
{
Lbl_responseinfo.text = ex. Message.tostring ();
}
Finally
{
if (Request!= null) request. GetRequestStream (). Close ();
if (response!= null) response. GetResponseStream (). Close ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.