如何在C#中获取受IP保护的网站的html源

时间:2019-03-12 14:01:14

标签: c# selenium selenium-webdriver c#-4.0 selenium-ide

我想为受https://www.homedepot.com/等受IP保护的网站加载HTML 在巴基斯坦无法访问此网站。

,我想获取HTML Source。我该怎么办?

我正在尝试下面给出的这段代码。

public HtmlAgilityPack.HtmlDocument LoadHtml(string url)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";
    request.AllowAutoRedirect = false;
    HtmlAgilityPack.HtmlDocument htmlDoc = null;
    //WebProxy myproxy = new WebProxy("198.12.118.99", 80);
    //myproxy.BypassProxyOnLocal = false;
    request.AllowAutoRedirect = true;
    //myproxy.Credentials = new NetworkCredential("dasani", "amazon1!");
    //request.Proxy = myproxy;

    try
    {
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.Load(response.GetResponseStream());
        response.Close();
    }
    catch (WebException ex)
    {
        ex.ToString();
        //MessageBox.Show(ex.ToString());
    }
    return htmlDoc;
}

0 个答案:

没有答案