Asp.net post XML document

Source: Internet
Author: User

Method 1:

Webrequest myhttpwebrequest = webrequest. Create ("http: // localhost: 4622/websitevip/searchservice. aspx ");
Myhttpwebrequest. method = "Post ";
// Send content
Stringbuilder sbpost = new stringbuilder ();
Sbpost. append ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> ");
Sbpost. append ("<requestinfo> ");
Sbpost. append ("<citycode> 0871 </citycode> ");
Sbpost. append ("<businesstype> 1 </businesstype> ");
Sbpost. append ("<tradetype> 12 </tradetype> ");
Sbpost. append ("<cardType> 1 </cardType> ");
Sbpost. append ("<discount> 8.0 </discount> ");
String keyword = "";
Sbpost. append ("<keywordstr>" + keyword + "</keywordstr> ");
Sbpost. append ("</requestinfo> ");
// Asciiencoding encoding = new asciiencoding (); encoding format
Utf8encoding encoding = new utf8encoding ();
String postdata = sbpost. tostring ();
Byte [] byte1 = encoding. getbytes (postdata );
Myhttpwebrequest. contenttype = "application/X-WWW-form-urlencoded ";
// Myhttpwebrequest. contenttype = "text/XML ";
Myhttpwebrequest. contentlength = byte1.length;
Stream newstream = myhttpwebrequest. getrequeststream ();
Newstream. Write (byte1, 0, byte1.length );
Newstream. Close ();
// Httpwebresponse response = myhttpwebrequest. getresponse ();
Httpwebresponse response = (httpwebresponse) myhttpwebrequest. getresponse ();

Method 2:

// String Server = "http: // localhost: 4622/websitevip/searchservice. aspx ";
// String pack = sbpost. tostring ();
// MSXML. xmlhttprequestclass XMLHTTP = new MSXML. xmlhttprequestclass ();
// Try
//{
// XMLHTTP. Open ("Post", server, false, type. Missing, type. Missing );
// XMLHTTP. Send (pack );
// String getresponseinfo = XMLHTTP. responsetext. tostring ();
// Return getresponseinfo;
//}
// Catch
//{
// Return "";
//}

MSXML reference: DLL downloaded from InterOP. MSXML. dll

 

============================

1. Send:

C # code
Webrequest myhttpwebrequest = webrequest. Create ("http://abc.com/xxx.aspx ");
// Set the 'method' property of the 'webrequest' to 'post '.
Myhttpwebrequest. method = "Post ";

// Create a New String object to post data to the URL.
String postdata = @ "<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Config>
<Type> in </type>
<Worktype> 2 </worktype>
</Config>
<DATA>
<Policy>
<Unitcode> branch code </unitcode>
<Applyno> warranty number </applyno>
<Applyendorseno> application number </applyendorseno>
</Policy>
</Data>
</Root> ";

Asciiencoding encoding = new asciiencoding ();
Byte [] byte1 = encoding. getbytes (postdata );

// Set the content type of the data being posted.
Myhttpwebrequest. contenttype = "application/X-WWW-form-urlencoded ";

// Set the content length of the string being posted.
Myhttpwebrequest. contentlength = byte1.length;

Stream newstream = myhttpwebrequest. getrequeststream ();

Newstream. Write (byte1, 0, byte1.length );
// Close the stream object.
Newstream. Close ();

Httpwebresponse response = myhttpwebrequest. getresponse ();

2. receive:

C # code
StreamReader reader = new StreamReader (Reqeust.InputStream);
String xml = reader.ReadToEnd();

1. Send:

C # code
Webrequest myhttpwebrequest = webrequest. Create ("http://abc.com/xxx.aspx ");
// Set the 'method' property of the 'webrequest' to 'post '.
Myhttpwebrequest. method = "Post ";

// Create a New String object to post data to the URL.
String postdata = @ "<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Config>
<Type> in </type>
<Worktype> 2 </worktype>
</Config>
<DATA>
<Policy>
<Unitcode> branch code </unitcode>
<Applyno> warranty number </applyno>
<Applyendorseno> application number </applyendorseno>
</Policy>
</Data>
</Root> ";

Asciiencoding encoding = new asciiencoding ();
Byte [] byte1 = encoding. getbytes (postdata );

// Set the content type of the data being posted.
Myhttpwebrequest. contenttype = "application/X-WWW-form-urlencoded ";

// Set the content length of the string being posted.
Myhttpwebrequest. contentlength = byte1.length;

Stream newstream = myhttpwebrequest. getrequeststream ();

Newstream. Write (byte1, 0, byte1.length );
// Close the stream object.
Newstream. Close ();

Httpwebresponse response = myhttpwebrequest. getresponse ();

2. receive:

C # code
StreamReader reader = new StreamReader (Reqeust.InputStream);
String xml = reader.ReadToEnd();
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.