1. 总结
总结2
3. Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.IO; using System.Web; namespace Core { public class RequestHelper { private Stream SendGetRequest1(string url) { string content; //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 is an abstract HttpWebRequest httpWebRequest;// Provides an HTTP-specific implementation of the System.Net.WebRequest class. HttpWebResponse httpWebResponse; //string url = "fsefsf"; httpWebRequest = WebRequest.Create(url) as HttpWebRequest; 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() as HttpWebResponse; Stream resStream = httpWebResponse.GetResponseStream(); using (StreamReader sr = new StreamReader(resStream)) { content = sr.ReadToEnd(); } if (httpWebResponse.StatusCode != HttpStatusCode.OK) { } return resStream; } private Stream SendGetRequestForStream(string url, string contentType) { HttpWebRequest httpWebRequest;// Provides an HTTP-specific implementation of the System.Net.WebRequest class. HttpWebResponse httpWebResponse; httpWebRequest = WebRequest.Create(url) as HttpWebRequest; httpWebRequest.Method = "GET"; httpWebRequest.ContentType = contentType; httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; Stream resStream = httpWebResponse.GetResponseStream(); return resStream; } private String SendGetRequest(string url, string contentType) { string content = ""; HttpWebRequest httpWebRequest; HttpWebResponse httpWebResponse; httpWebRequest = WebRequest.Create(url) as HttpWebRequest; httpWebRequest.Method = "GET"; httpWebRequest.ContentType = contentType;//"application/x-www-form-encoded"; httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; Stream resStream = httpWebResponse.GetResponseStream(); using (StreamReader sr = new StreamReader(resStream)) { content = sr.ReadToEnd(); } return content; } private Stream SendPostRequestForStream(string url, string data, string contentType) { //string content = ""; HttpWebRequest httpWebRequest; HttpWebResponse httpWebResponse; httpWebRequest = WebRequest.Create(url) as HttpWebRequest; httpWebRequest.Method = "POST"; httpWebRequest.ContentType = contentType; Stream reqStream = httpWebRequest.GetRequestStream(); using (StreamWriter sw = new StreamWriter(reqStream)) { sw.Write(data); } httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; Stream ResStream = httpWebResponse.GetResponseStream(); return ResStream; } private String SendPostRequest(string url, string data, string contentType) { string content = ""; HttpWebRequest httpWebRequest; HttpWebResponse httpWebResponse; httpWebRequest = WebRequest.Create(url) as HttpWebRequest; httpWebRequest.Method = "POST"; httpWebRequest.ContentType = contentType; Stream reqStream = httpWebRequest.GetRequestStream(); using (StreamWriter sw = new StreamWriter(reqStream)) { sw.Write(data); } httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; Stream resStream = httpWebResponse.GetResponseStream(); using (StreamReader sr = new StreamReader(resStream)) { content = sr.ReadToEnd(); } return content; } } }