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 ;}