【发布时间】:2012-07-10 01:36:42
【问题描述】:
问题
我的 c# Web 请求检索到的 html 页面与我使用浏览器获得的不同。
详情
我正在尝试获取此 URL 引用的页面的 HTML:
https://sistemas.usp.br/jupiterweb/listarGradeCurricular?codcg=12&codcur=12012&codhab=1&tipo=N
我用于 WebRequest 的代码是这个:
public string HttpsGet (string url)
{
string response = string.Empty;
if (!string.IsNullOrEmpty(url))
{
HttpWebRequest WReq = (HttpWebRequest)WebRequest.Create("https://uspdigital.usp.br/jupiterweb/listarGradeCurricular?codcg=9&codcur=9012&codhab=100&tipo=N");
WReq.Credentials = CredentialCache.DefaultCredentials;
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
try
{
WReq.Proxy = new WebProxy();
WReq.Method = "GET";
WReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.107 Safari/535.1";
WReq.ServicePoint.ConnectionLimit = 800;
WReq.Timeout = 80000;
WReq.ContentType = "application/x-www-form-urlencoded";
WReq.Referer = "";
WReq.AllowAutoRedirect = true;
HttpWebResponse resp = (HttpWebResponse)WReq.GetResponse();
using (resp)
{
response = (new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("ISO-8859-1"))).ReadToEnd();
}
}
catch (Exception exception)
{
Exception ex = exception;
}
return response;
}
else
{
throw new Exception("URL is empty or null");
}
}
我是如何发现它们不同的
我将从代码中检索到的 html 和浏览器中的 html(在 chrome 上查看源代码)粘贴到 notepad++ 上。
在那之后,我设法“计数”(ctrl+f -> 计数)这个字符串“#CCCCCC”,它代表 某些表格行的背景颜色。
webrequest 的计数为 17,而浏览器的计数为 14。
此外,每个页面的“课程”都不同:网络请求课程是“Faculdade de Ciências Farmacêuticas”,而浏览器上的课程是“Faculdade de Economia, Administração e Contabilidade”(这些名称是葡萄牙语)。
TL:DR
不知道为什么,点击这个链接:https://uspdigital.usp.br/jupiterweb/listarGradeCurricular?codcg=12&codcur=12012&codhab=1&tipo=N 在 webrequest c# 中给了我一个不同的页面,与我复制并粘贴到浏览器时的结果相比。
更新
我尝试比较两个请求中的用户代理,它们匹配。
我发现通过 C# 的 Web 请求总是给我相同的页面,即“Faculdade de Ciências Farmacêuticas”课程的页面
我猜这与 HTTPS 的事情有关。
先谢谢了,很抱歉发了这么长的帖子
【问题讨论】:
-
您指定的用户代理 - 它与您用于在浏览器中查看的用户代理相同吗?我猜 uspdigital.usp.br 的服务器会根据浏览器返回不同的视图?
标签: c# html webrequest