Httpwebrequest and httpwebresponse

Source: Internet
Author: User

Httpwebrequest

The httpwebrequest class supports the attributes and methods defined in webrequest and the additional attributes and methods that allow users to directly interact with servers using HTTP.
Do not use httpwebrequest constructor. Use the webrequest. Create method to initialize a new httpwebrequest object.

Httpwebresponse

You must never directly create an instance of the httpwebresponse class. Instead, use the instance returned by calling httpwebrequest. getresponse. You must call the stream. Close method or httpwebresponse. Close method to close the response and release the connection for reuse. You do not need to call stream. Close and httpwebresponse. close at the same time, but this will not cause errors.

Sample Code

Public String httprequest (string URL, string XML) {string MSG = string. empty; byte [] bytes = encoding. utf8.getbytes (XML); httpwebrequest request = (httpwebrequest) webrequest. create (URL); Request. preauthenticate = true; request. allowwritestreambuffering = true; request. sendchunked = true; request. protocolversion = httpversion. version11; request. useragent = "Mozilla/4.0 (compatible; MSIE 7.0; window S nt 5.2 ;. net CLR 1.1.4322 ;. net CLR 2.0.50727) "; request. method = "Post"; request. contentlength = bytes. length; request. contenttype = "text/XML"; request. headers. add ("Authorization", "Basic" + convert. tobase64string (New asciiencoding (). getbytes ("username: Password"); // credentialcache mycredential = new credentialcache (); mycredential. add (New uri (URL), "Basic", new networkcredential ("User Name "," password "); Request. credentials = mycredential; // send data using (Stream requeststream = request. getrequeststream () {requeststream. write (bytes, 0, bytes. length);} // return response httpwebresponse response = (httpwebresponse) request. getresponse (); If (response. statuscode! = Httpstatuscode. OK) {MSG = string. format ("post failed {0}", response. statuscode);} else {stream responsestream = response. getresponsestream (); streamreader sr = new streamreader (responsestream, encoding. getencoding ("GBK"); MSG = sr. readtoend (); Sr. close (); response. close ();} return MSG ;}

 

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.