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

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;
        }

  

posted @ 2012-11-22 16:43  xyzhuzhou  阅读(153)  评论(0编辑  收藏  举报