欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET使用HttpWebRequest读取远程网页源代码

程序员文章站 2023-12-17 13:42:58
读取远程网页能做什么就不用多说了吧,做小偷程序或是采集,也就诸如此类了吧。 public string getpage(string url) { h...

读取远程网页能做什么就不用多说了吧,做小偷程序或是采集,也就诸如此类了吧。

public string getpage(string url)
{

  httpwebrequest request = null;

  httpwebresponse response = null;

  streamreader reader = null;

  try
  {

    request = (httpwebrequest)webrequest.create(url);

    request.timeout = 20000;

    request.allowautoredirect = false;

    response = (httpwebresponse)request.getresponse();

    if (response.statuscode == httpstatuscode.ok && response.contentlength < 1024 * 1024)
    {

      reader = new streamreader(response.getresponsestream(), system.text.encoding.default);

      string html = reader.readtoend();

      return html;

    }

  }

  catch
  {

  }

  finally
  {

    if (response != null)
    {

      response.close();

      response = null;

    }

    if (reader != null)

      reader.close();

    if (request != null)

      request = null;

  }

  return string.empty;
}

上一篇:

下一篇: