Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#身份验证失败,因为远程方已关闭传输流_C#_Authentication_Stream_Transport_Party - Fatal编程技术网

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

C#身份验证失败,因为远程方已关闭传输流,c#,authentication,stream,transport,party,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++) {

我想指出的是,我已经为此寻找了很多,但没有找到解决方案。因此,我创建了一个循环,它将抛出包含链接的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;
    }
private void按钮4\u单击(对象发送者,事件参数e)
{
尝试
{
对于(int i=0;i
下面的错误是在循环命中各种URL时触发的,例如()。奇怪的是,如果我删除循环并对该站点进行http GET请求,一切都会正常工作

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

我不认为证书有问题,因为否则在删除循环时仍然会出现错误


任何建议都将不胜感激。

从您的评论来看,zvoxaudio.com的url相当棘手。他们有一个机器人检测系统,可以将物品添加到购物车中。你特别使用的链接似乎无论如何都不起作用,我认为它使用的是过期的商品id;我可以通过将id更新为4007001并将其更改为“https”来使用它。但是,此链接正在将项目添加到购物车中,并且站点不允许机器人将项目添加到购物车中,站点返回400错误,标题中的key:value为:

X-Error-Message: Bots are not allowed to add items to cart
尝试向请求中添加UserAgent,如下所示:

  myRequest.UserAgent = "Nada";
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();
    }
  }
}
有了这些改动,ZVOXAUDIO链接就可以工作了

如果您想将url保留为“https”请求,那么只需将其添加到代码中即可

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

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

  myRequest.UserAgent = "Nada";
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();
    }
  }
}
建议您在循环之外设置服务器证书验证回调和安全协议,因为它是所有请求的静态设置。另外,如果您使用C#using语法,Dispose方法会为您关闭和处理WebResponse和StreamReader变量。C#在.NET3.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;
那么您的两种方法如下所示:

  myRequest.UserAgent = "Nada";
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();
    }
  }
}
private void按钮4\u单击(对象发送者,事件参数e)
{
尝试
{
对于(var i=0;i

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

这是我的想法,但对我来说,这是由于安全协议问题

只要加上这一行

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

更多信息->

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


ServicePointManager.SecurityProtocol=(SecurityProtocolType)3072

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

自创建凭据以来已过了1年,因此我想需要续订


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

只需调用web请求上方的以下功能即可:

    protected void Application_Start()
    {
        if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
        {
            ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
        }  
       
    }
调试时,在