如何使用WebRequest的访问加密使用https网站的SSL? [英] How do I use WebRequest to access an SSL encrypted site using https?

查看:170
本文介绍了如何使用WebRequest的访问加密使用https网站的SSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序,从用户提供的URL读取内容。我的问题是在code,它是这样的:

I'm writing a program that reads content from a user provided URL. My problem is in the code that goes something like this:

Uri uri = new Uri(url);
WebRequest webRequest = WebRequest.Create(uri);
WebResponse webResponse = webRequest.GetResponse();
ReadFrom(webResponse.GetResponseStream());

如果提供的这个是打破的网址的是一个https://开头URL。谁能帮我改变这个code,使其与SSL加密的内容合作。谢谢你。

And this is breaking if the provided url is an "https://" URL. Can anyone help me with changing this code so that it will work with SSL encrypted content. Thanks.

推荐答案

您正在做正确的方式,但用户可能会提供的URL安装了无效的SSL证书的网站。如果你把这个线你做出实际的Web请求之前,你可以忽略这些证书的问题:

You're doing it the correct way but users may be providing urls to sites that have invalid ssl certs installed. You can ignore those cert problems if you put this line in before you make the actual web request:

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

在这里AcceptAllCertifications被定义为

where AcceptAllCertifications is defined as

public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}

这篇关于如何使用WebRequest的访问加密使用https网站的SSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆