zoukankan      html  css  js  c++  java
  • 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 "";
            }
  • 相关阅读:
    Python学习笔记Day24
    Python学习笔记Day23
    Python学习笔记Day22
    Python学习笔记Day21
    Python学习笔记Day19
    Python学习笔记Day18
    Python学习笔记Day17
    Python学习笔记Day16
    Python学习笔记Day15
    linux普通文件权限和系统目录权限的实践及结论
  • 原文地址:https://www.cnblogs.com/simadi/p/6490237.html
Copyright © 2011-2022 走看看