【问题标题】:C# Http Web Request failing to certain sites [closed]C# Http Web 请求对某些站点失败[关闭]
【发布时间】:2018-07-12 09:39:29
【问题描述】:

我尝试使用 c# http 请求连接到 awesemo.com,但它的 http 连接失败并显示以下错误: Error1: IOException: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。 Error2: SocketException: 现有连接被远程主机强行关闭。

我尝试访问包括 Google.com 在内的许多其他网站都运行良好,但上面的网站和一个 squarespace 网站连接失败。但是一个月前一切正常。这是我面临的新问题。我尝试了很多测试不同的代码以及更改框架但仍然无法修复。

这是我的代码: (除了注释行之外的所有行都是在使用 chrome 浏览器成功向上述网站发出 Web 请求后从 fiddler 复制的。)

    private void test()
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.awesemo.com/");

        request.KeepAlive = true;
        request.Headers.Add("Upgrade-Insecure-Requests", @"1");
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
        //request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
        request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9");
        request.Headers.Set(HttpRequestHeader.Cookie, @"_ga=GA1.2.523834838.1531216658; __stripe_mid=93850985-96a8-491e-b6ee-8c4b3147f3ab; _tccl_visitor=1fa9b919-5361-46be-9fb2-00cccf35c249");
        //System.Net.ServicePointManager.Expect100Continue = false;
        //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }

【问题讨论】:

  • 这是网站广告吗?
  • 阅读How to Ask 并展示您的尝试。如果您想以编程方式访问网站但它拒绝这样做,您必须确保您的请求看起来像是合法请求。将来自成功请求的所有标头与失败的请求进行比较。
  • @Pepitosh 不,不是广告。如果你这么认为,我删除了链接。
  • 对我也有用,也许无论你做了什么导致他们阻止了你的 IP 地址。
  • @mjwills 谢谢。是的控制台应用程序工作正常,但在 winform 中它不工作。

标签: c# httpwebrequest fiddler webrequest httpwebresponse


【解决方案1】:

经过大量测试,我发现问题出在 .NET 框架版本上。框架版本是 4.5,在我更新到 4.6 后,它解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多