Asp.net WebClient.DownloadString似乎更改了外部站点的一些html

Asp.net WebClient.DownloadString似乎更改了外部站点的一些html,asp.net,html,webclient,Asp.net,Html,Webclient,我有一个ASP.NET网站(.aspx),我从ASP.NET MVC 4移动网站(.cshtml)中调用它以获取其html响应字符串。这两个站点都托管在Windows Server 2008 R2系统上。它们是使用VS2010 Professional创建和发布的 -如果我直接访问外部站点并查看源代码,那么它是正确的 -如果我使用以下任何一种方式获取外部html: using (WebClient client = new WebClient()) { html =

我有一个ASP.NET网站(.aspx),我从ASP.NET MVC 4移动网站(.cshtml)中调用它以获取其html响应字符串。这两个站点都托管在Windows Server 2008 R2系统上。它们是使用VS2010 Professional创建和发布的

-如果我直接访问外部站点并查看源代码,那么它是正确的

-如果我使用以下任何一种方式获取外部html:

 using (WebClient client = new WebClient())
     {
         html = client.DownloadString(strUrl);
     }

然后更改html(在父表上设置字体系列):

直接访问站点时,我会得到以下请求标题:

Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: .ASPXBrowserOverride=Mozilla%2f4.0+(compatible%3b+MSIE+6.0%3b+Windows+CE%3b+IEMobile+8.12%3b+MSIEMobile+6.0); 
编辑:

如果我在调试模式下查看客户机对象,则在调用DownloadString之前和之后,client.Headers都是空的。 此外,在调用DownloadString之后,这里是client.responseHeader:

{Content-Length: 267123
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Tue, 27 Nov 2012 18:37:27 GMT
Set-Cookie: ASP.NET_SessionId=******; path=/; HttpOnly
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
}
解决方案:

不幸的是,我不能接受两个答案。伊卡洛斯和詹姆斯·劳鲁克的回答都帮助我解决了这个问题。我正在根据最近的结果选择答案,最终得出解决方案。谢谢你们两位

简而言之,解决方案如下:

使用fiddler查看请求头并查找用户代理。 修改代码如下:

using (WebClient client = new WebClient())
     {
         client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
         html = client.DownloadString(strUrl);
     }

正如杜尔在评论中指出的那样,很可能是浏览器嗅探,因为
WebClient
根本不会更改生成的HTML


如果使用Fiddler并以与
WebClient
完全相同的方式设置请求头,您可能可以验证这一点。我打赌你会得到同样的HTML输出

尝试设置用户代理值,并尝试使用不同的浏览器。这可能证明网站正在根据用户代理头切换HTML响应

webClient.Headers.Add("user-agent", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5");
var iphoneHtml = webClient.DownloadString("http://www.yoursite.com");
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
var safariHtml = webClient.DownloadString("http://www.yoursite.com");

它是什么类型的站点?Windows与Linux托管?什么CMS?等等。@James Lawruk-我用这些信息更新了我的问题。我发布了fiddler的请求头,但它们看起来是一样的。@Soenhay
WebClient
的UserAgent头怎么会有这样的:User-Agent:Mozilla/5.0(Windows NT 6.0)AppleWebKit/537.11(KHTML,像Gecko)Chrome/23.0.1271.64 Safari/537.11" ?
WebClient
不会添加该标题。你确定你看到的是由
WebClient
发送的正确标题吗?我是fiddler的新手,所以我可能看错了东西。。。我刚刚从fiddler中删除了所有会话,然后刷新了网页,这样fiddler中就有一行了。然后我点击它,这就是请求标题下的内容。。。。如果我对在调试模式下运行的项目执行相同的操作,那么fiddler中还有其他几行,但是它们都有相同的UserAgent。嘿!这样做,但将其设置为我在fiddler中找到的用户代理似乎已经解决了我的问题。。。。。“Mozilla/5.0(Windows NT 6.0)AppleWebKit/537.11(KHTML,类似Gecko)Chrome/23.0.1271.64 Safari/537.11”。。。。。。。。这是什么意思?我认为这可能是一个坏主意,为了将来的证明,硬编码的用户代理,但我不确定。默认情况下,Webclient不发送用户代理头,所以网站必须有代码来响应,通过发送不同的标记。
GET / HTTP/1.1
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: .ASPXBrowserOverride=Mozilla%2f4.0+(compatible%3b+MSIE+6.0%3b+Windows+CE%3b+IEMobile+8.12%3b+MSIEMobile+6.0); 
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: .ASPXBrowserOverride=Mozilla%2f4.0+(compatible%3b+MSIE+6.0%3b+Windows+CE%3b+IEMobile+8.12%3b+MSIEMobile+6.0); 
{Content-Length: 267123
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Tue, 27 Nov 2012 18:37:27 GMT
Set-Cookie: ASP.NET_SessionId=******; path=/; HttpOnly
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
}
using (WebClient client = new WebClient())
     {
         client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
         html = client.DownloadString(strUrl);
     }
webClient.Headers.Add("user-agent", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5");
var iphoneHtml = webClient.DownloadString("http://www.yoursite.com");
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
var safariHtml = webClient.DownloadString("http://www.yoursite.com");