C# 模拟浏览器请求

      public string getHtml(string Url, string type = "UTF-8")
        {
            try
            {
                System.Net.HttpWebRequest wReq = (HttpWebRequest)System.Net.HttpWebRequest.Create(Url);
                wReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0";     //  。。。
                System.Net.HttpWebResponse wResp = (HttpWebResponse)wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(type)))
                {
                    return reader.ReadToEnd().Replace("\n", "").Replace("\r", "");
                }
            }
            catch (System.Exception ex)
            {
            }
            return "";
        }

posted @ 2016-06-12 14:45  —阿峰—  阅读(2257)  评论(0编辑  收藏  举报