【C#】HTTP请求GET,POST(远程证书失效)
HTTP定义了与服务器交互的不同方法,基本方法有GET,POST,PUT,DELETE,分别对于查,该,增,删。一般情况下我们只用到GET和POST,其他两种都也可以用GET和POST来实现,很多浏览器也只实现了GET和POST两种方法
GET:用户获取信息
安全的:不会对服务器上的数据进行修改,和破坏
幂等的:相同的请求所返回的结果相同
参数是通过Url来传输,接在Url后面中间用?分开,不同参数之间用&分开,下面表示传递两个参数param1和param2,值为value1和value2
http://songurl/index.htm?param1=value1¶m2=value2
通过上面可以看到,GET请求数据在隐私方面又是不安全的,因为Url上的信息可以直接让用户看到,这样不利于隐私保护
POST:用于更新数据
传递的数据对用户隐藏,封装在包里,具体看演示
有时候请求会重定向,但我们就需要从重定向url获取东西,这时可以设置重定向禁用,你就可以从headers的Location属性中获取重定向地址
request.AllowAutoRedirect = false;
string[] values = request.Headers.GetValues("Location");
下面是辅助类的定义
public class HttpHelper { /// <summary> /// 创建GET方式的HTTP请求 /// </summary> public static HttpWebResponse CreateGetHttpResponse(string url, int timeout, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; //http版本,默认是1.1,这里设置为1.0 } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "GET"; //设置代理UserAgent和超时 //request.UserAgent = userAgent; //request.Timeout = timeout; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } return request.GetResponse() as HttpWebResponse; } /// <summary> /// 创建POST方式的HTTP请求 /// </summary> public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int timeout, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; //request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; //设置代理UserAgent和超时 //request.UserAgent = userAgent; //request.Timeout = timeout; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //发送POST数据 if (!(parameters == null || parameters.Count == 0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); i++; } } byte[] data = Encoding.ASCII.GetBytes(buffer.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } string[] values = request.Headers.GetValues("Content-Type"); return request.GetResponse() as HttpWebResponse; } /// <summary> /// 获取请求的数据 /// </summary> public static string GetResponseString(HttpWebResponse webresponse) { using (Stream s = webresponse.GetResponseStream()) { StreamReader reader = new StreamReader(s, Encoding.UTF8); return reader.ReadToEnd(); } } /// <summary> /// 验证证书 /// </summary> private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) return true; return false; } }