Http post xml data

Source: Internet
Author: User

I wrote an article about httppost.ArticleHttp://www.cnblogs.com/qidian10/archive/2011/06/20/2085341.html

This articleCodeIn fact, it is a bit of a problem: the first time the data is submitted, it will not pass, and then try again is no problem, it has been puzzled, until today on the msdn found a solution, paste the code below:

Protected String Httppostdata ( String URL, String Strparm, Int Outtime)
{
Stringbuilder Str = New Stringbuilder ();
Try
{
Httpwebrequest wrequest;
Httpwebresponse wresponse = Null ;
Httpwebresponse response = Null ;
// Preauth the request
// You can add logic so that you only pre-authenticate the very first request.
// You shoshould not have to pre-authenticate each request.
Uri URI = New Uri (URL );
Wrequest = (Httpwebrequest) httpwebrequest. Create (URI );
Wrequest. Credentials = New Networkcredential ( " Wcadmin " , " Wcadmin " );
Wrequest. preauthenticate = True ;
Wrequest. useragent = " Upload PLM XML " ;
Wrequest. Method = " Head " ;
Wrequest. Timeout = Outtime;
Wresponse = (Httpwebresponse) wrequest. getresponse ();
Wresponse. Close ();
// Make the real request.
Wrequest = (Httpwebrequest) httpwebrequest. Create (URL );
Wrequest. Credentials = New Networkcredential ( " Wcadmin " , " Wcadmin " );
Wrequest. preauthenticate = True ;
Wrequest. useragent = " Upload PLM XML " ;
Wrequest. Method = " Post " ;
Wrequest. allowwritestreambuffering = False ;
Wrequest. Timeout = 10000 ;
Byte [] postbytes = Encoding. utf8.getbytes (strparm );
Wrequest. contentlength = Postbytes. length;
Stream requeststream = Wrequest. getrequeststream ();
Requeststream. Write (postbytes, 0 , Postbytes. Length );
Requeststream. Close ();
Try
{
Response = (Httpwebresponse) wrequest. getresponse ();
}
Catch (Webexception E)
{
Response = (Httpwebresponse) E. response;
}
Stream responsestream = Response. getresponsestream ();
Streamreader = New Streamreader (responsestream, encoding. Default );
While (Streamreader. Peek () ! = - 1 )
{
Str. append (streamreader. Readline ());
}
Streamreader. Close ();
Response. Close ();
Wresponse. Close ();
}
Catch (Exception error)
{
Str. appendline (error. Message );
}
Return Str. tostring ();
}

Note that you can use this method to submit data in the English comments without encountering an inexplicable error 400.

If the problem persists, refer to the following code.

Http://www.cnblogs.com/hyl8218/archive/2011/07/04/2097394.html

There are two methods to add the HTTP basic authentication information to the request when sending the request:

    • First, add authorization to the Request Header:
      Authorization: "basic user name and passwordBase64Encrypted string"
    • The second is to add the user name and password to the URL:
      Http ://Username: password @Api.minicloud.com.cn/statuses/friends_timeline.xml

The following shows the implementation code for the first language that adds the Authorization header to the request.

First look at. Net:

 String  Username  =  "  Username "  ;
String Password = " Password " ;
// Note that the format here is "username: Password"
String Usernamepassword = Username + " : " + Password;
Credentialcache mycache = New Credentialcache ();
Mycache. Add ( New Uri (URL ), " Basic " , New Networkcredential (username, password ));
Myreq. Credentials = Mycache;
Myreq. headers. Add ( " Authorization " , " Basic " + Convert. tobase64string ( New Asciiencoding (). getbytes (usernamepassword )));

Webresponse WR = Myreq. getresponse ();
Stream receivestream = Wr. getresponsestream ();
Streamreader = New Streamreader (receivestream, encoding. utf8 );
String Content = Reader. readtoend ();

You can also use httpwebrequest or other classes to send requests.

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.