当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET使用HttpWebRequest读取远程网页源代码

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

吴显国近况,人猿杂交,影子写手

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

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;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网