zoukankan      html  css  js  c++  java
  • 用asp.net c# 获取网页源代码

    方法一:webrequest

     private string GetStringByUrl(string strUrl)
        {
            WebRequest wrt = WebRequest.Create(strUrl);
            WebResponse wrse = wrt.GetResponse();
            Stream strM = wrse.GetResponseStream();
            StreamReader SR = new StreamReader(strM, Encoding.GetEncoding("gb2312"));
            string strallstrm = SR.ReadToEnd();
            return strallstrm;
        }

    方法二:HttpWebRequest

     public static string GetPage(string url, Encoding encoding)

            {

                HttpWebRequest request
    = null;

                HttpWebResponse response
    = null;

                StreamReader reader
    = null;

               
    try

                {

                    request
    = (HttpWebRequest)WebRequest.Create(url);

                    request.UserAgent
    = "www.svnhost.cn";

                    request.Timeout
    = 20000;

    request.AllowAutoRedirect
    = false;



                    response
    = (HttpWebResponse)request.GetResponse();

                   
    if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)

                    {

                        reader
    = new StreamReader(response.GetResponseStream(), encoding);

                       
    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;

            }

  • 相关阅读:
    SE Springer小组《Spring音乐播放器》软件需求说明3
    SE Springer小组之《Spring音乐播放器》可行性研究报告三、四
    软件工程学习笔记一:单元测试
    关于软件工程
    离散数学中的命题表达式计算并生成真值表
    “A + B”竟然还能这样做?
    测试程序运行时间的方法——clock()
    排序(1)———选择排序及其优化
    临时存几张图
    伊利亚特
  • 原文地址:https://www.cnblogs.com/luluping/p/1347373.html
Copyright © 2011-2022 走看看