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;

            }

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yefengzhixia/archive/2009/07/01/4312127.aspx

  • 相关阅读:
    经典卷积神经网络算法(4):GoogLeNet
    经典卷积神经网络算法(3):VGG
    经典卷积神经网络算法(2):AlexNet
    经典卷积神经网络算法(1):LeNet-5
    卷积神经网络入门篇
    人工神经网络实践
    Python操作Oracle数据库:cx_Oracle
    源码剖析@ApiImplicitParam对@RequestParam的required属性的侵入性
    实战SpringCloud通用请求字段拦截处理
    Elasticsearch系列---生产集群部署(下)
  • 原文地址:https://www.cnblogs.com/jazzka702/p/1530989.html
Copyright © 2011-2022 走看看