zoukankan      html  css  js  c++  java
  • 读取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;
    }

  • 相关阅读:
    sublime text 4 vim 插件配置
    ssh-keygen 的使用
    distribution transaction solution
    bilibili 大数据 视频下载 you-get
    Deepin 20.2.1 安装 MS SQL 2019 容器版本
    【转】使用Linux下Docker部署MSSQL并加载主机目录下的数据库
    【转】You Can Now Use OneDrive in Linux Natively Thanks to Insync
    dotnet 诊断工具安装命令
    Linux 使用 xrandr 设置屏幕分辨率
    【转】CentOS 7.9 2009 ISO 官方原版镜像下载
  • 原文地址:https://www.cnblogs.com/jdk123456/p/3090388.html
Copyright © 2011-2022 走看看