【问题标题】:matweb.com: How to get source of page?matweb.com:如何获取页面来源?
【发布时间】:2010-12-20 20:11:38
【问题描述】:

我有这样的网址:
http://www.matweb.com/search/DataSheet.aspx?MatGUID=849e2916ab1541be9ff6a17b78f95c82

我想使用此代码从该页面下载源代码:

private static string urlTemplate = @"http://www.matweb.com/search/DataSheet.aspx?MatGUID=";

static string GetSource(string guid)
{
    try
    {
        Uri url = new Uri(urlTemplate + guid);

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Method = "GET";               

        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

        Stream responseStream = webResponse.GetResponseStream();
        StreamReader responseStreamReader = new StreamReader(responseStream);
        String result = responseStreamReader.ReadToEnd();

        return result;
    }
    catch (Exception ex)
    {
        return null;
    }
}

当我这样做时,我得到:

您似乎没有启用 cookie。 MatWeb 需要启用 cookie。

好的,我明白了,所以我添加了几行:

CookieContainer cc = new CookieContainer();  
webRequest.CookieContainer = cc; 

我明白了:

您的 IP 地址因过度使用而受到限制。当公司中的许多人或通过互联网服务提供商共享 IP 地址时,问题可能会更加复杂。对于给您带来的任何不便,我们深表歉意。

我可以理解这一点,但是当我尝试使用网络浏览器访问此页面时,我没有收到此消息。我该怎么做才能获得源代码?一些 cookie 或 http 标头?

【问题讨论】:

    标签: c# cookies httpwebrequest http-headers


    【解决方案1】:

    它可能不喜欢你的 UserAgent。试试这个:

    webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"; //maybe substitute your own in here
    

    【讨论】:

    • 哦,谢谢!这样可行 :)。顺便说一句,我怎样才能获得自己的 UserAgent?
    • 要获取浏览器发送的 UserAgent,请尝试以下站点:whatsmyuseragent.com
    【解决方案2】:

    如果您收到“过度使用”的回复,您似乎在做公司不喜欢的事情。

    【讨论】:

    • 他说他只有在尝试运行他的程序时才会收到此消息。所以他的 IP 不是问题。
    • 看起来是用户代理,见上。
    【解决方案3】:

    您下载页面的速度太快了。

    当您使用浏览器时,您每秒可能会看到最多一页。使用应用程序,您每秒可以获得几页,这可能是他们的 Web 服务器正在检测的内容。因此过度使用。

    【讨论】:

    • 这不是真的,因为即使我运行此函数一次,我也会收到此消息。所以太快的城镇加载不是问题。
    猜你喜欢
    • 2016-05-25
    • 2014-09-30
    • 2012-08-03
    • 2019-05-25
    • 2014-05-03
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多