'System.Net.WebException' 访问 WebClient 时。在浏览器上运行良好
'System.Net.WebException' when accessing WebClient. Works fine on browser
我想去网站下载一个字符串,我制作了这个 php 文件来展示示例。
(这不适用于我的整个网站)
link http://swageh.co/information.php 不会从任何 PC 使用 webClient 下载。
我更喜欢使用网络客户端。
无论我尝试什么,它都不会下载字符串。
它在浏览器上运行良好。
它 returns 错误 500 'System.Net.WebException' 类型的未处理异常发生在 System.dll
附加信息:底层连接已关闭:发送时发生意外错误。是错误
这是因为您的网站正在响应 301 永久移动
见 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 的回答而不是公认的答案。
有趣的是,这不起作用 - 尝试使 headers 与 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();
您是否更改了服务器端的某些内容?
到目前为止,以下所有选项对我来说都很好用(所有 return 只是 "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;
我想去网站下载一个字符串,我制作了这个 php 文件来展示示例。
(这不适用于我的整个网站)
link http://swageh.co/information.php 不会从任何 PC 使用 webClient 下载。
我更喜欢使用网络客户端。 无论我尝试什么,它都不会下载字符串。 它在浏览器上运行良好。
它 returns 错误 500 'System.Net.WebException' 类型的未处理异常发生在 System.dll
附加信息:底层连接已关闭:发送时发生意外错误。是错误
这是因为您的网站正在响应 301 永久移动 见 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 的回答而不是公认的答案。
有趣的是,这不起作用 - 尝试使 headers 与 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();
您是否更改了服务器端的某些内容?
到目前为止,以下所有选项对我来说都很好用(所有 return 只是 "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;