今天,有个网友问我:
用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# asp.net http HttpWebRequest模拟浏览器请求下载文件到本地
Asp.Net使用HttpWebRequest跨域上传,可以实现asp.net跨域文件上传或者大数传输
解决火车头7.6网站报错System.Net.HttpWebRequest问题的处理 插件 无限制。 https.dll 永久使用无限制版本https
VB.NET httpwebrequest 例子 适合入门
VB.NET实现的谷歌翻译接口,使用了HttpWebRequest,可以自动解压gzip数据流(webcilent不方便),以及json解析,运行JScript代码,EXECL,Word关联操作
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
下面小编就为大家带来一篇C# httpwebrequest访问HTTPS错误处理方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本示例使用C#构造SOAP信息,通过HttpWebRequest调用java编写的带有Windows身份验证的WebService,代码中详细注释了每行代码的功能与作用; 对应文章:http://blog.csdn.net/cgs_______/article/details/77894599
.net通过HttpWebRequest模拟表单通过name/value形式提交参数和文件实例上传文件 通过HttpWebRequest类模拟网页请求,可向服务端提交多参数和多文件数据,参数和文件已key、value的形式进行上传。已集成完整的类库,...
.NET中封装静态类通过HttpWebRequest实现post、get、文件传输。
C#中HttpWebRequest使用介绍 GET HTTP HTTPS 请求
httpwebrequest调用webservice返回数据
C#使用HttpWebRequest自动更新客户端应用程序
本文分享了一个使用HttpWebRequest读取远程网页的案例,供大家参考学习。
使用 HTTP Client 请求 HTTPS 的 API 时出现 The certificate cannot be verified up to a trusted certification authority 异常,并且证书已经传入。 下面就是问题代码: public class Program { public static...
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes =...
使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 timeouts, cookies, headers, protocols。另一个好处是HttpWebRequest类不会阻塞UI线程。例如,当您从响应很慢的API服务器下载大文件时,您的应用...
使用vs2008 vb.net编写的下载网页的代码,使用httpwebrequest方法,完全可用
C#(也称Csharp)在多线程下并发执行HTTP请求的实现,采用C#封装HttpWebRequest类开发的多线程并发采集程序源码文档,文档中详细说明了HttpWebRequest并发HTTP请求实现网站采集的方法,经过测试同时并发1000+不是问题...