C# POST Https请求的一些坑
2016-04-15 14:20
281 查看
写在前面:
从上次,跟合作方的站点对接开始就产生了这个问题,当时用C#进行POST提交,总是会出现问题,找了很久发现对方的站点居然是TLS 1.2 的。正文:
![](https://images2015.cnblogs.com/blog/141063/201604/141063-20160415141324285-2075236160.jpg)
然而,在.NET FrameWork 4.0的环境下,居然找不到。。。System.Net.SecurityProtocolType 这个枚举,没有这个值。。。
所以,在POST提交的时候,是会出现问题,有的网站就不会有这个问题,因为他们是1.0的。
![](https://images2015.cnblogs.com/blog/141063/201604/141063-20160415141503941-935740150.jpg)
所以啊,感觉这就是一个坑,好在,即使没有现成的,1.2我们也是可以用代码来实现1.2的
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //SecurityProtocolType.Tls1.2;
当然,如果是4.0以后的环境,查看这个枚举是可以看到不同的值的。
namespace System.Net { using System; [Flags] public enum SecurityProtocolType { Ssl3 = 0x30, Tls = 0xc0, Tls11 = 0x300, Tls12 = 0xc00 } }
,,,,到这里,该说的,都说了,最后附上,C# https POST的代码吧。
class ProgramTest { static void Main(string[] args) { string url = "https://www.test.com"; string result = PostUrl(url, "key=123"); // key=4da4193e-384b-44d8-8a7f-2dd8b076d784 Console.WriteLine(result); Console.WriteLine("OVER"); Console.ReadLine(); } private static string PostUrl(string url, string postData) { HttpWebRequest request = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { request = WebRequest.Create(url) as HttpWebRequest; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request.ProtocolVersion = HttpVersion.Version11; // 这里设置了协议类型。 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; request.KeepAlive = false; ServicePointManager.CheckCertificateRevocationList = true; ServicePointManager.DefaultConnectionLimit = 100; ServicePointManager.Expect100Continue = false; } else { request = (HttpWebRequest)WebRequest.Create(url); } request.Method = "POST"; //使用get方式发送数据 request.ContentType = "application/x-www-form-urlencoded"; request.Referer = null; request.AllowAutoRedirect = true; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Accept = "*/*"; byte[] data = Encoding.UTF8.GetBytes(postData); Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); //获取网页响应结果 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); string result = string.Empty; using (StreamReader sr = new StreamReader(stream)) { result = sr.ReadToEnd(); } return result; } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } }
相关文章推荐
- HTTP referer说明
- HTTP协议详解
- HTTP 表单编码 enctype
- 标记一篇好的文章(socket tcp)
- 递归神经网络不可思议的有效性
- 读书笔记-神经网络与深度学习(一)-使用神经网络识别手写数字
- 非监督版bp网络
- Muduo网络库源码分析(四)EventLoopThread和EventLoopThreadPool的封装
- WAP调用微信支付https://pay.weixin.qq.com/wiki/doc/api/wap.php?chapter=15_1
- httpget请求在android4.4系统出现java.net.ProtocolException: Unexpected status line:
- TCP报文
- ZOJ 2314 有上下界的网络流
- 神经网络学习笔记-问题记录(一)
- 【NodeJs】使用TCP套接字收发数据的简单实例
- HTTP方法 谓词 及在nginx中如何关闭(limit_except) 【转】
- HTTP方法 谓词 及在nginx中如何关闭(limit_except) 【转】
- redhat6.5 简易编译安装httpd服务
- HttpUrlConnection的简单使用--get和post的简单使用
- 《TCP/IP具体解释卷2:实现》笔记--IP:网际协议
- android中Http访问时 connection.getResponseCode()不被执行