• 生成一个连接对象 生成静态页


    public static string GetDataFromUrl(string url)
            {
                string str = string.Empty;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                //设置Http头;
                request.AllowAutoRedirect = true;
                request.AllowWriteStreamBuffering = true;
                request.Referer = "";
                request.Timeout = 10 * 1000;
                //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        //根据http应答头来判别编码
                        string Characterset = response.CharacterSet;
                        Encoding encode;
                        if (Characterset != "")
                        {
                            if (Characterset == "ISO-8859-1")
                            {
                                Characterset = "gb2312";
                            }
                            encode = Encoding.GetEncoding(Characterset);
                        }
                        else
                        {
                            encode = Encoding.Default;
                        }
                        //声明一个内存流来贮存http应答流
                        Stream Receivestream = response.GetResponseStream();
                        MemoryStream mstream = new MemoryStream();
                        byte[] bf = new byte[255];
                        int count = Receivestream.Read(bf, 0, 255);
                        while (count > 0)
                        {
                            mstream.Write(bf, 0, count);
                            count = Receivestream.Read(bf, 0, 255);
                        }
                        Receivestream.Close();
                        mstream.Seek(0, SeekOrigin.Begin);
                        //从内存流里读取字符串这里涉及到了编码方案
                        StreamReader reader = new StreamReader(mstream, encode);
                        char[] buf = new char[1024];
                        count = reader.Read(buf, 0, 1024);
                        while (count > 0)
                        {
                            str += new string(buf, 0, 1024);
                            count = reader.Read(buf, 0, 1024);
    
                        }
                        reader.Close();
                        mstream.Close(); 
                    }
    
                }
                catch (Exception ex)
                {
    
                    GetDataFromUrl(url);
    
                }
                finally
                {
                    if (response != null)
                        response.Close();
                }
    
                return str;
            }
    

      

  • 相关阅读:
    vue与laravel
    php artisan 命令
    HTTP 状态码
    PhpStorm提高效率的使用方法及设置
    好RESTful API的设计原则
    laravel 入门基础之安装
    c++ sizeof(字符数组)
    new delete/delete[] 测试
    linux g++ 查找动态链接库
    linux下定时器耗时研究
  • 原文地址:https://www.cnblogs.com/xyzhuzhou/p/2730531.html
Copyright © 2020-2023  润新知