[转]C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
2010-02-22 20:26
951 查看
用httpwebrequest访问一个SSL类型的地址 https://xxxx 时,报错 “未能为 SSL/TLS 安全通道建立信任关系(Could not establish trust relationship for the SSL/TLS secure channel)”
查了下MSDN,找到了解决方法,SSL网站,连接时需要提供证书,对于非必须提供客户端证书的情况,只要返回一个安全确认即可。但是此方法的实现,在.NET 1.1 和 .NET 2.0 下是不同的,下面写出2个framework版本下的实现方法:
使用的命名空间:
using System.Net; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates;
.Net 2.0
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { //直接确认,否则打不开 return true; } private void button1_Click(object sender, EventArgs e) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); HttpWebRequest req = (HttpWebRequest)WebRequest.CreateDefault(new Uri("https://zu14.cn/")); req.Method = "GET"; HttpWebResponse res = (HttpWebResponse)req.GetResponse(); //...正常使用了,和访问普通的 http:// 地址一样了
.Net 1.1
internal class AcceptAllCertificatePolicy : ICertificatePolicy { public AcceptAllCertificatePolicy() { } public bool CheckValidationResult(ServicePoint sPoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest wRequest, int certProb) { //直接确认 return true; } } private void button1_Click(object sender, EventArgs e) { ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy(); HttpWebRequest req = (HttpWebRequest)WebRequest.CreateDefault(new Uri("https://zu14.cn/")); req.Method = "GET"; HttpWebResponse res = (HttpWebResponse)req.GetResponse(); //...正常使用了,和访问普通的 http:// 地址一样了
相关文章推荐
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
- c# .net 4.0 HttpWebRequest 访问https TLS1.2 解决方案
- VB.net使用httpWebRequest访问带cookie的网站时如何传递cookie值
- C# 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- c#第一篇 在WPF的window窗体中使用httpwebrequest实现模拟登陆网页,并在webbroser控件中显示
- c# .net core HttpWebRequest 使用Headers[]方法代替UserAgent
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- asp.net[web.config] httphandlers 与实现自由定义访问地址
- vb.net如何使用HttpWebRequest模拟登陆带验证码的网站
- C# 使用HttpWebRequest 实现文件的上传
- asp.net[web.config] httphandlers 与实现自由定义访问地址
- C# httpwebrequest访问HTTPS错误处理
- 【C#MVC】 使用HttpWebRequest访问外部API,并返回json信息 (Access_Key)
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端