c# 获取网页源代码(支持cookie),最简单代码

/// /// 获取网页源码 

 public static string GetHtmls(string url, string referer = "", string cookie = "", string codeStr = "utf-8")
{
var wc = new WebClient { Credentials = CredentialCache.DefaultCredentials };
try { var nv = new NameValueCollection {
{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"},
{"Content-Type", "application/x-www-form-urlencoded"}
//Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
//Content-Type application/x-www-form-urlencoded
};

if (referer.Length > 0) { nv.Add("Referer", referer); }
if (cookie.Length > 0) { nv.Add("Cookie", cookie); }
wc.Headers.Add(nv); Byte[] pageData = wc.DownloadData(url);
Encoding enc = Encoding.GetEncoding(codeStr);
return enc.GetString(pageData);
}
catch (Exception e) {
TextTool.Log(e, e.GetType().Name + " 获取源代码出错 " + url); return "";
}
finally { wc.Dispose(); } }

------------------------------------------------------------

 

        public string GetHtmls(string url,string charSet= "gb2312")
        {
            try
            {
                var message = new HttpClient().GetAsync(new Uri(url)).Result;
                var contentType = message.Content.Headers.ContentType;
                if (string.IsNullOrEmpty(contentType.CharSet))
                {
                    contentType.CharSet = charSet;
                }
                return message.Content.ReadAsStringAsync().Result;
            }
            catch (Exception exception)
            {
                TextTool.Log(exception);
            }
            return "";
        }

 

posted @ 2017-03-02 13:57  simadi  阅读(1837)  评论(0编辑  收藏  举报