C# 身份验证失败,因为远程方已关闭传输流



我想指出,我已经搜索了很多,但没有解决方案。因此,我创建了一个循环,每次创建http GET请求以访问完整的源代码时,该循环将抛出包含链接的listBox2。

我的代码:

private void button4_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < listBox2.Items.Count; i++)
{
code(listBox2.Items[i].ToString() + "'");
//await PutTaskDelay();
//Console.WriteLine(listBox2.Items[i].ToString());
if (VarrileKeeper.s.ToLower().Contains("mysql"))
{
Console.WriteLine("possitive " + listBox2.Items[i].ToString());
}
}
}
catch (Exception Fejl)
{
Console.WriteLine(Fejl);
}
}

public static String code(string Url)
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
myRequest.MaximumAutomaticRedirections = 4;
myRequest.MaximumResponseHeadersLength = 4;
myRequest.Credentials = CredentialCache.DefaultCredentials;
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();

StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Dispose();
myResponse.Close();
VarrileKeeper.s = result;
return result;
}

当循环命中各种 url 时会触发以下错误,例如 (http://www.memphremagog.org/fr/lexique.php?id=32)。奇怪的是,如果我删除循环并对该网站发出 http GET 请求,一切正常。

类型为"System.Net.WebException"的第一次机会异常发生在 System.dll System.Net.WebException:底层连接是 已关闭:发送时发生意外错误。--->System.IO.IOException:身份验证失败,因为远程方 已关闭传输流。

我不认为这是证书的问题,否则当我删除循环时,我仍然会在 http://www.memphremagog.org/fr/lexique.php?id=32 上收到错误。

任何建议不胜感激。

从您的评论来看,zvoxaudio.com 网址非常棘手。 他们有一些机器人检测,用于将商品添加到购物车。 您专门使用的链接似乎无论如何都不起作用,我认为它使用的是过期的项目 ID;我能够通过将 id 更新为 4007001 并将其更改为"https"来使用它。 但是,此链接正在将商品添加到购物车,并且该站点不允许机器人将商品添加到购物车,该站点返回 400 错误,并在标题中显示以下键:值:

X-Error-Message: Bots are not allowed to add items to cart

尝试将用户代理添加到您的请求中,如下所示:

myRequest.UserAgent = "Nada";

通过这些一些更改,ZVOXAUDIO链接就可以工作了!

如果你想将网址保留为"https"请求,那么只需将其添加到你的代码中。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

这是必需的,因为ZVOXAUDIO不支持TLS 1.0或更早的协议。 如果您使用的.NET版本不支持TLS 1.2,则可以使用SecurityProtocolType.Tls11,因为ZVOXAUDIO仍然支持TLS 1.1。

但是,由于您可能正在尝试在尽可能多的站点上运行它,因此您可能不希望只允许TLS 1.2,因为较旧的服务器可能不支持它。 我会像这样设置您的安全协议:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls |
SecurityProtocolType.Ssl3;

作为建议,您可以在循环之外设置服务器证书验证回调和安全协议,因为它是所有请求的静态设置。 此外,如果使用语法使用 C#,则 Dispose 方法会负责关闭和释放 WebResponse 和 StreamReader 变量。 C# 在 .NET 3.0 中引入了"var",您可能希望开始接受它,即假设您使用的是 3.0 或更高版本的框架。 如果你想看看这会是什么样子,请看下面。

确保您有以下用途:

using System;
using System.IO;
using System.Net;

然后将这两行放在表单或对象的静态构造函数中,我假设您现在正在使用表单:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

那么你的两个方法将如下所示:

private void button4_Click(object sender, EventArgs e)
{
try
{
for (var i = 0; i < listBox2.Items.Count; i++)
{
var response = Code(listBox2.Items[i].ToString() + "'");
if (response.ToLower().Contains("mysql"))
{
Console.WriteLine("positive " + listBox2.Items[i].ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

public static string Code(string url)
{
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.MaximumAutomaticRedirections = 4;
webRequest.MaximumResponseHeadersLength = 4;
webRequest.UserAgent = "Mozilla/5.0 (Taco2) Gecko/20100101";
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.Method = "GET";
using (var webResponse = webRequest.GetResponse())
{
using (var sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}

祝您编码愉快! 请随时在下面的评论中提出任何问题。

这是由于安全协议问题而发生的。只需在请求上下文之前添加以下行即可。

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

这发生在我身上,但对我来说,这是由于安全协议问题。

只需添加此行

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

更多信息 -> https://codeshare.co.uk/blog/how-to-fix-the-error-authentication-failed-because-the-remote-party-has-closed-the-transport-stream/

就我而言,问题出在我使用的邮件服务(mailgun)的凭据上。

自创建凭据以来已经过去了 1 年,所以我想有必要续订。

重置凭据密码为我解决了这个问题。

只需在 Web 请求上方调用以下函数:

protected void Application_Start()
{
if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
{
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}  

}

最新更新