C#利用HttpWebRequest进行post请求的示例(HTTPS)
1 HttpWebRequest 方式
C#利用HttpWebRequest进行post请求的示例(HTTPS)
2 WebClient 方式
static string PostData(string url, NameValueCollection headers, NameValueCollection data) { if (url.StartsWith("https")) { // 解决WebClient不能请求https url的问题 System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; }; } WebClient client = new WebClient(); client.Headers.Add(headers); byte[] byteArr = client.UploadValues(url, data); string result = Encoding.UTF8.GetString(byteArr); return result; }
3 c# .net 4.0 HttpWebRequest 访问https TLS1.2 解决方案(推荐这个)
c# .net 4.0 HttpWebRequest 访问https TLS1.2 解决方案
// 在HttpWebRequest或WebClient前面加上这个
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;