C# 从URL抓取HTML不会';不行-有什么建议吗?

C# 从URL抓取HTML不会';不行-有什么建议吗?,c#,C#,我在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></

我在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>"
尝试:

        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 { }
尝试:

        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。你可以试试这样的

webClient.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

这个特定的头可能不起作用,但您可以尝试其他类似标准浏览器的头。

我检查了这个URL,您需要解析cookies

当您第一次尝试访问该页面时,您将被重定向到login.live.com上的https URL,然后重定向回原始URL。https页面为域login.live.com设置一个名为
MSPRequ
的cookie。如果您没有此cookie,则无法访问该站点

我尝试在浏览器中禁用cookies,结果它无限循环回到URL
https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&checkda=1&ct=1328303901&rver=6.1.6195.0&wp=MBI&wreply=http:%2F%2Fwww.windowsphone.com%2Fen-US%2Fgames%3Flist%3Dxbox&lc=1033&id=268289
。这已经持续了几分钟,而且似乎永远不会停止


因此,当设置好cookie后,您必须从https页面获取cookie,并为后续请求保留该cookie。

您将获得HTML。HTML是web服务器响应的标记代码。你想得到屏幕截图吗?你想在不同的应用程序中嵌入web浏览器吗?Nick,我想要HTML。使用上述方法获取的HTML不会返回WebBrowser返回的HTML?请尝试向请求添加适当的UserAgent,如果请求似乎不是来自合法的web浏览器,有时这些网站不允许访问。您好,它们使用元标记将用户重定向到页面。您得到的是服务器的正确响应。正如drew010所说,他们可能会试图阻止屏幕抓取程序访问该网站。此外,如果您检索该网站的方式无法执行Javascript,那么您仍然会运气不佳。看起来这可能是个问题。谢谢Nick,我将尝试将标题添加到webclient。如果这一个不起作用,我会尝试一些其他的东西,以及使用标题信息。谢谢德鲁,这看起来是完全正确的。。但这已经超出了我的理解范围:(我将尝试使用浏览器对象获取它。我不想这样做,因为我不需要所有花哨的图形,只需要简单的HTML,但它会完成工作。我发现这显示了如何扩展webclient,以便它可以为您保留cookies。这可能会有所帮助。再次感谢。尝试使用该类,但结果相同。