Webrequest post XML

Source: Internet
Author: User

Webrequest request = Webrequest. Create ( " Http://www.contoso.com/PostAccepter.aspx " );
// Set the method property of the request to post.
Request. Method = " Post " ;
// Create post data and convert it to a byte array.
String Postdata = " This is a test that posts this string to a Web server. " ;
Byte [] 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.
Stream 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.
Webresponse 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.
Streamreader = New Streamreader (datastream );
// Read the content.
String Responsefromserver = Reader. readtoend ();
// Display the content.
Console. writeline (responsefromserver );
// Clean up the streams.
Reader. Close ();
Datastream. Close ();
Response. Close ();
Q.This is an example in msdn. It is okay to use this method to post general characters, but an XML string of post is as follows:
System. xml. xmldocument xmlpostdata = new system. xml. xmldocument ();
Xmlpostdata. Load ("D: \ SS \ test. xml ");
String postdata = xmlpostdata. innerxml;

In webresponse response = request. getresponse ();

Error message: webexceptionstatus protocolerror

A.Set string postdata = xmlpostdata. innerxml;
Change
String postdata = httputility. htmlencode (xmlpostdata. innerxml );

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.