HttpWebRequest and HttpWebResponse Summary

Source: Internet
Author: User

1. Summary

Summary 2

3. Code

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Net;usingSystem.IO;usingsystem.web;namespacecore{ Public classRequesthelper {PrivateStream SendGetRequest1 (stringURL) {            stringcontent; //HttpRequest request;//enables ASP. NET to read the HTTP values sent by a client during a WEB request.WebRequest WebRequest;//makes a request to a Uniform Resource Identifier (URI). this was an abstractHttpWebRequest HttpWebRequest;//provides an http-specific implementation of the System.Net.WebRequest class.HttpWebResponse HttpWebResponse; //string url = "FSEFSF";HttpWebRequest = webrequest.create (URL) asHttpWebRequest; HttpWebRequest.AllowAutoRedirect=true; Httpwebrequest.method="GET"; Httpwebrequest.contenttype="application/x-www-form-urlencoded"; //HttpWebRequest.            ContentType = "Application/json"; //HttpWebRequest.            ContentType = "Application/xml"; //HttpWebRequest. Headers.add ("url", url);HttpWebResponse= HttpWebRequest.GetResponse () asHttpWebResponse; Stream Resstream=Httpwebresponse.getresponsestream (); using(StreamReader sr =NewStreamReader (Resstream)) {Content=Sr.            ReadToEnd (); }            if(Httpwebresponse.statuscode! =Httpstatuscode.ok) {}returnResstream; }        PrivateStream Sendgetrequestforstream (stringUrlstringContentType) {HttpWebRequest HttpWebRequest;//provides an http-specific implementation of the System.Net.WebRequest class.HttpWebResponse HttpWebResponse; HttpWebRequest= WebRequest.Create (URL) asHttpWebRequest; Httpwebrequest.method="GET"; Httpwebrequest.contenttype=ContentType; HttpWebResponse= HttpWebRequest.GetResponse () asHttpWebResponse; Stream Resstream=Httpwebresponse.getresponsestream (); returnResstream; }        PrivateString Sendgetrequest (stringUrlstringContentType) {            stringContent ="";            HttpWebRequest HttpWebRequest;            HttpWebResponse HttpWebResponse; HttpWebRequest= WebRequest.Create (URL) asHttpWebRequest; Httpwebrequest.method="GET"; Httpwebrequest.contenttype= ContentType;//"application/x-www-form-encoded";HttpWebResponse = HttpWebRequest.GetResponse () asHttpWebResponse; Stream Resstream=Httpwebresponse.getresponsestream (); using(StreamReader sr =NewStreamReader (Resstream)) {Content=Sr.            ReadToEnd (); }            returncontent; }        PrivateStream Sendpostrequestforstream (stringUrlstringDatastringContentType) {            //String content = "";HttpWebRequest HttpWebRequest;            HttpWebResponse HttpWebResponse; HttpWebRequest= WebRequest.Create (URL) asHttpWebRequest; Httpwebrequest.method="POST"; Httpwebrequest.contenttype=ContentType; Stream Reqstream=Httpwebrequest.getrequeststream (); using(StreamWriter SW =NewStreamWriter (Reqstream)) {SW.            Write (data); } HttpWebResponse= HttpWebRequest.GetResponse () asHttpWebResponse; Stream Resstream=Httpwebresponse.getresponsestream (); returnResstream; }        PrivateString Sendpostrequest (stringUrlstringDatastringContentType) {            stringContent ="";            HttpWebRequest HttpWebRequest;            HttpWebResponse HttpWebResponse; HttpWebRequest= WebRequest.Create (URL) asHttpWebRequest; Httpwebrequest.method="POST"; Httpwebrequest.contenttype=ContentType; Stream Reqstream=Httpwebrequest.getrequeststream (); using(StreamWriter SW =NewStreamWriter (Reqstream)) {SW.            Write (data); } HttpWebResponse= HttpWebRequest.GetResponse () asHttpWebResponse; Stream Resstream=Httpwebresponse.getresponsestream (); using(StreamReader sr =NewStreamReader (Resstream)) {Content=Sr.            ReadToEnd (); }            returncontent; }    }}
View Code

HttpWebRequest and HttpWebResponse Summary

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.