C# GetResponse()在有效URl上返回404

C# GetResponse()在有效URl上返回404,c#,web,httpwebrequest,screen-scraping,httpwebresponse,C#,Web,Httpwebrequest,Screen Scraping,Httpwebresponse,我试图通过C#应用程序抓取网页,但它一直在响应 “远程服务器返回错误:(404)未找到。” 该网页可以通过浏览器访问,但应用程序一直失败。谢谢你的帮助 var d = DateTime.UtcNow.Date; var AddressString = @"http://www.booking.com/searchresults.html?src=searchresults&si=ai%2Cco%2Cci%2Cre%2Cdi&ss={0}&checkin_mont

我试图通过C#应用程序抓取网页,但它一直在响应

“远程服务器返回错误:(404)未找到。”

该网页可以通过浏览器访问,但应用程序一直失败。谢谢你的帮助

var d = DateTime.UtcNow.Date;
var AddressString = @"http://www.booking.com/searchresults.html?src=searchresults&si=ai%2Cco%2Cci%2Cre%2Cdi&ss={0}&checkin_monthday={1}&checkin_year_month={2}&checkout_monthday={3}&checkout_year_month={4}";
var URi = String.Format(AddressString, "Prague", d.Day, d.Year + "-" + d.Month, d.Day + 1, d.Year + "-" + d.Month);
var request = (HttpWebRequest)WebRequest.Create(URi);
request.Timeout = 5000;
request.UserAgent = "Fiddler"; //I tried to set next three rows not to be null
request.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = WebProxy.GetDefaultProxy();
try
{
    var response = (HttpWebResponse)request.GetResponse();
}
catch(WebException e)
{
    var response = (HttpWebResponse)e.Response; //e.Response contains WebPage, but it is incomplete
    StreamReader sr = new StreamReader(response.GetResponseStream());
    HtmlDocument doc = new HtmlDocument();
    doc.Load(sr);
    var a = doc.DocumentNode.SelectNodes("div[@class='resut-details']"); //fails, as not all desired nodes arent in response
 }
编辑:

嗨,伙计们,谢谢你们的建议

根据DavidMartins的回答,我添加了标题:“接受编码:gzip,deflate,sdch”,但它本身并没有帮助

我使用Fidler试图获取有关该问题的任何信息,但我第一次看到该应用程序,它并没有让我变得更聪明。另一方面,我试图将request.UserAgent更改为我的浏览器发送的内容(“用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/34.0.1847.131 Safari/537.36”;)瞧,我再也没有收到404异常,但文档不可读,因为它充满了这样的咒语:?½O~ï?½ï?½ï?Gï½。我尝试设置request.transferncode=“UTF-8”,但要启用此属性,request.SendChunked必须设置为true,以

协议违反例外

其他信息:无法为不写入数据的操作设置内容长度或分块编码

编辑2:
我忘了什么,我想不出是什么。我得到了某种编码的响应,需要先对其进行解码才能正确读取。即使在Fiddler中,当我想看到响应时,我也需要确认解码以检查结果。在我用fiddler解码后,我得到了我想要进入我的应用程序的东西…

所以,在尝试了Jon Skeet和David Martin的建议后,我找到了更进一步的地方,并在另一个toppic中找到了新问题的相关答案。如果有人曾经寻找过类似的东西,答案如下:


我建议您使用Fiddler2或Wireshark查看网络上的内容。如果web服务器发送404响应,则WebRequest的行为完全正确。