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

Tac*_*co2 12 c# authentication stream transport party

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

我的代码:

 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;
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

任何建议表示赞赏.

Twi*_*tem 14

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

X-Error-Message: Bots are not allowed to add items to cart
Run Code Online (Sandbox Code Playgroud)

尝试将UserAgent添加到您的请求中,如下所示:

  myRequest.UserAgent = "Nada";
Run Code Online (Sandbox Code Playgroud)

通过这些改动,ZVOXAUDIO链接可以正常工作!

如果您想将网址保留为"https"请求,请将其添加到您的代码中.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Run Code Online (Sandbox Code Playgroud)

这是必需的,因为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;
Run Code Online (Sandbox Code Playgroud)

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

确保你有这些用途:

using System;
using System.IO;
using System.Net;
Run Code Online (Sandbox Code Playgroud)

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

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
Run Code Online (Sandbox Code Playgroud)

然后你的两个方法看起来像这样:

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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

快乐的编码!请在下面的评论中随意提出任何问题.