简体   繁体   English

C#HttpWebRequest显示404,但在浏览器中站点可访问

[英]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.

相关问题 C#HttpWebRequest与浏览器 - C# HttpWebRequest vs browser 使用C#(httpwebrequest)将POST调用到外部站点 - Invoking a POST to an external site with C# (httpwebrequest) httpwebrequest中的C#Web浏览器Cookie - c# web browser cookies in httpwebrequest 为什么链接“@”在我的浏览器中工作,但C#HttpWebRequest给出404(远程服务器返回错误:(405)Method Not Allowed。) - Why does a link with “@” work in my browser but C# HttpWebRequest gives 404 (The remote server returned an error: (405) Method Not Allowed.) C#-httpwebrequest状态代码返回200而不是404 - c# - httpwebrequest status code returns 200 instead of 404 c#HttpWebRequest 404,对象不完整列表返回null - c# HttpWebRequest 404, return null on object not entire List C#HttpWebRequest.GetResponse()返回错误:(404)找不到 - c# HttpWebRequest.GetResponse() returned an error: (404) Not Found C# HttpWebRequest Medium 博客返回 403 Forbidden but Site is Open - C# HttpWebRequest Medium Blog returns 403 Forbidden but Site is Open C#HTTPWebRequest与领域网站进行协商/基本 - C# HTTPWebRequest against Negotiate/Basic with Realm Site C#使用HTTPWebRequest拉取网页并从站点执行javascript - C# Pull a webpage with HTTPWebRequest and execute the javascript from the site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM