【发布时间】:2012-02-03 20:56:32
【问题描述】:
我在 C# 中使用 webclient 和 webresponse 尝试了几种方法,它们都返回
<html><head><meta http-equiv=\"REFRESH\" content=\"0; URL=http://www.windowsphone.com/en-US/games?list=xbox\"><script type=\"text/javascript\">function OnBack(){}</script></head></html>"
而不是当你使用浏览器去http://www.windowsphone.com/en-US/games?list=xbox时实际呈现的页面
您将如何从该位置获取 HTML? http://www.windowsphone.com/en-US/games?list=xbox
谢谢!
/edit:添加示例:
试过了:
string inputUrl = "http://www.windowsphone.com/en-US/games?list=xbox";
string resultHTML = String.Empty;
Uri inputUri = new Uri(inputUrl);
WebRequest request = WebRequest.CreateDefault(inputUri);
request.Method = "GET";
WebResponse response;
try
{
response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
resultHTML = reader.ReadToEnd();
}
}
catch { }
试过了:
string inputUrl = "http://www.windowsphone.com/en-US/games?list=xbox";
string resultHTML = String.Empty;
WebClient webClient = new WebClient();
try
{
resultHTML = webClient.DownloadString(inputUrl);
}
catch { }
试过了:
string inputUrl = "http://www.windowsphone.com/en-US/games?list=xbox";
string resultHTML = String.Empty;
WebResponse objResponse;
WebRequest objRequest = HttpWebRequest.Create(inputUrl);
try
{
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
resultHTML = sr.ReadToEnd();
sr.Close();
}
}
catch { }
【问题讨论】:
-
您正在获取 HTML。 HTML 是 Web 服务器响应的标记代码。您是否正在寻找屏幕截图?您是否希望在不同的应用程序中嵌入网络浏览器?
-
尼克,我想要 HTML。我使用提到的方法获得的 HTML 不返回我的 WebBrowser 返回的 HTML?
-
尝试在请求中添加适当的 UserAgent,如果请求似乎不是来自合法的网络浏览器,有时这些站点不允许访问。
-
嗨,他们使用元标记将用户重定向到页面。你得到的是来自服务器的正确响应。正如draw010所说,他们可能会尝试阻止屏幕抓取工具访问该网站。
-
另外,如果您检索它的方式无法执行 Javascript,那么您仍然会不走运。看起来这可能是个问题。
标签: c#