我正在尝试使用c#从网站下载xml文件,但我在某些网址上获得了404。这是有线的,因为它们仍然在浏览器中工作。其他网址仍然没有问题。
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());
此网址是上述问题网址之一:
http://numerique.bibliotheque.toulouse.fr/cgi-bin/oaiserver?verb=ListMetadataFormats
已解决....忘了修剪网址;)
答案 0 :(得分:3)
我只能推测主机网站可能不喜欢您的UserAgent
并且正在返回404消息
答案 1 :(得分:2)
我用这个解决了这个问题:
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;
答案 2 :(得分:1)
要下载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);
答案 3 :(得分:0)
也许
1)不知何故,你输入了错误的网址:你可以试试吗? 把
WebRequest.Create(@"http://numerique.bibliotheque.toulouse.fr/cgi-bin/oaiserver?verb=ListMetadataFormats");
而不是
WebRequest.Create(url);
用于测试目的。
2)你有一些HTTP过滤机制,可以区分VS&浏览器要求