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

时间:2017-03-25 12:48:02

标签: 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;
    }

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

  

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

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

任何建议都表示赞赏。

5 个答案:

答案 0 :(得分:13)

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

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

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

  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#using语法,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();
    }
  }
}

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

答案 1 :(得分:1)

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

ServicePointManager.SecurityProtocol =(SecurityProtocolType)3072;

答案 2 :(得分:0)

这是我自己想到的,但是对我来说,这是由于安全协议问题造成的。

只需添加此行

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/

答案 3 :(得分:0)

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

自创建凭据以来已经过去了一年,所以我认为有必要进行续订。

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

答案 4 :(得分:0)

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

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