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