[英]C# HttpWebRequest shows 404, but site is reachable in browser
I am trying to download an xml file from a website with c#, but I get an 404 on some urls. 我正在尝试使用C#从网站下载xml文件,但在某些网址上却收到404。 this is wired because they still work in the browser.
这是有线的,因为它们仍然可以在浏览器中工作。 Other urls still work without a problem.
其他网址仍然可以正常使用。
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 3000;
request.UserAgent = "Test Client";
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)
request.GetResponse();
}
catch (WebException e)
{
response = (HttpWebResponse)e.Response;
}
Console.WriteLine("- "+response.StatusCode);
XmlTextReader reader = XmlTextReader(response.GetResponseStream());
This URL is one of the said problem URLs: 此URL是上述问题URL之一:
http://numerique.bibliotheque.toulouse.fr/cgi-bin/oaiserver?verb=ListMetadataFormats
SOLVED....forgot to trim the url ;) 求助....忘记修剪网址;)
我只能推测宿主站点可能不喜欢您的UserAgent
并且正在返回404消息
I solved this problem by using this: 我通过使用以下方法解决了这个问题:
var client = (HttpWebRequest)WebRequest.Create(uri);
client.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
client.CookieContainer = new CookieContainer();
client.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
var htmlCodae = client.GetResponse() as HttpWebResponse;
For downloading xml document you can use DownloadString method: 要下载xml文档,可以使用DownloadString方法:
System.Net.WebClient client = new System.Net.WebClient();
String url = "http://stackoverflow.com/feeds/question/4188449";
String xmlSource = client.DownloadString(url);
Console.WriteLine(xmlSource);
Maybe 也许
1) Somehow you input incorrect url: can you try to put 1)以某种方式输入了错误的网址:您可以尝试输入
WebRequest.Create(@"http://numerique.bibliotheque.toulouse.fr/cgi-bin/oaiserver?verb=ListMetadataFormats");
instead of 代替
WebRequest.Create(url);
for testing purpose. 用于测试目的。
2) You have some HTTP filtering mechanism which distinguishes between VS & browser requrests 2)您有一些HTTP过滤机制可以区分VS和浏览器要求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.