WebRequest.GetResponse错误:底层连接 - C#Asp.Net

时间:2018-04-09 09:53:52

标签: c# asp.net

以下是我的HttpWebRequest代码。我想知道为什么我可以在google.com或facebook.com上使用此代码。

在以下特定网站上,我收到此错误<input type="text" name="username" value="{{ old('username') }}">

我需要在代码中添加什么才能使其工作?你们有个主意吗?我搜索了我需要证书或可信证书来发出http请求,但是如何在我的代码上实现这一点?

The underlying connection was closed: An unexpected error occurred on a send.

2 个答案:

答案 0 :(得分:1)

使用CreateHttp静态助手方法而不是Create。

原因是你有一个属性ServerCertificateValidationCallback,它是一个委托,允许你自定义认证验证的动作。

在下面的情况中,我已将其设置为静态返回true以继续或允许请求。

HttpWebRequest MyWebRequest;
WebResponse MyWebResponse;
StreamReader sr;
string strHTML;
StreamWriter sw;
MyWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://qiita.com/naoki_mochizuki/items/3fda1ad6594c11d7b43c");
MyWebRequest.ServerCertificateValidationCallback = (snd, cert, chain, err) => true;

答案 1 :(得分:1)

我添加了此代码来解决此问题

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;