• 读取Internet网站上的某些内容


    当需要用到某个网站上的一些内容时,可以使用下面的方法,获取整个网页的内容,然后在截取自己想要的那部分

    private string GetWebContent(string Url)
    {
                string strResult = "";
                try
                {

          //声明一个HttpWebRequest请求

          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                    //设置连接超时时间
                   request.Timeout = 30000;
                    request.Headers.Set("Pragma", "no-cache");
                    //这个一定要加上,在某些网站没有会发生"远程服务器返回错误: (403) 已禁止。"错误
                    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)";
                    //rq.Accept = "*/*";
                    //rq.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
                    request.Method = "GET";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream streamReceive = response.GetResponseStream();
                    Encoding encoding = Encoding.GetEncoding("GB2312");
                    StreamReader streamReader = new StreamReader(streamReceive, encoding);
                    strResult = streamReader.ReadToEnd();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return strResult;
    }

  • 相关阅读:
    Yii2 简单DateTimePicker
    Yii2简单的 yii2-phpexcel导出
    Yii2.0 是如何引入js和css
    Yii2.0 behaviors方法使用
    Yii2.0 Activeform表单部分组件使用方法
    Yii Url重新
    Yii CModel中rules验证规则
    Yii 1.0 伪静态即Yii配置Url重写(转)
    Yii main配置文件解析
    Yii框架 phpexcel 导出
  • 原文地址:https://www.cnblogs.com/jdk123456/p/3090388.html
Copyright © 2020-2023  润新知