【问题标题】:'System.Net.WebException' when accessing WebClient. Works fine on browser访问 WebClient 时出现“System.Net.WebException”。在浏览器上运行良好
【发布时间】:2018-07-19 15:16:12
【问题描述】:

我想去一个网站下载一个字符串,我做了这个php文件来展示一个例子。

(这不适用于我的整个网站)

链接http://swageh.co/information.php 不会从任何PC 使用webClient 下载。

我更喜欢使用 webClient。 无论我尝试什么,它都不会下载String。 它在浏览器上运行良好。

返回错误 500 System.dll 中发生“System.Net.WebException”类型的未处理异常

附加信息:底层连接已关闭:发送时发生意外错误。是错误

【问题讨论】:

  • 网站在返回要下载的文件之前检查您的会话和标头并不少见,也许可以使用邮递员之类的工具尝试一下,这样您就可以轻松地修改标头。
  • 请指定返回错误500的错误信息(如果有)
  • 请出示您正在使用的代码。
  • @deztructicus System.dll 中出现“System.Net.WebException”类型的未处理异常附加信息:底层连接已关闭:发送时发生意外错误。

标签: c# webclient


【解决方案1】:

您是否在服务器端进行了一些更改?
到目前为止,以下所有选项对我来说都可以正常工作(所有选项都只返回“false”,StatusCode 为 200):

 var client = new WebClient();
 var stringResult = client.DownloadString("http://swageh.co/information.php");

还有HttpWebRequest:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://swageh.co/information.php");
 request.GetResponse().GetResponseStream();

较新的HttpClient:

var client = new HttpClient();

var req = new HttpRequestMessage(HttpMethod.Get, "http://swageh.co/information.php");

var res = client.SendAsync(req);

var stringResult = res.Result.Content.ReadAsStringAsync().Result;

【讨论】:

  • 是的,当您在 Chrome 中打开链接时,现在没有 301 重定向。
  • 像一个魅力一样工作,仍然不知道为什么它突然停止返回 301,因为我没有更改任何代码。
【解决方案2】:

这是因为您的网站响应 301 Moved Permanently 见Get where a 301 URl redirects to 这显示了如何自动跟随重定向:Using WebClient in C# is there a way to get the URL of a site after being redirected? 看看 Christophe Debove 的答案,而不是公认的答案。

有趣的是,这不起作用 - 尝试使标题与下面的 Chrome 相同,也许使用 Telerik Fiddler 来查看发生了什么。

var strUrl = "http://theurl_inhere";
            var headers = new WebHeaderCollection();
            headers.Add("Accept-Language", "en-US,en;q=0.9");
            headers.Add("Cache-Control", "no-cache");
            headers.Add("Pragma", "no-cache");
            headers.Add("Upgrade-Insecure-Requests", "1");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Method = "GET";
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.Accept = "text/html,application/xhtml+xml,application/xml; q = 0.9,image / webp,image / apng,*/*;q=0.8";
            request.Headers.Add( headers );
            request.AllowAutoRedirect = true;
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();

            var strLastRedirect = response.ResponseUri.ToString();

            StreamReader reader = new StreamReader(dataStream);
            string strResponse = reader.ReadToEnd();

            response.Close();

【讨论】:

  • 很遗憾,它对我不起作用。但我确实得到了错误代码。System.dll 中发生了“System.Net.WebException”类型的未处理异常附加信息:底层连接已关闭:发送时发生意外错误。以常规 webClient 的形式返回准确的错误代码。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多