zoukankan      html  css  js  c++  java
  • HttpHeplp 公共类 HttpWebRequest

    public class HttpHelp
        {
            public CookieContainer CookieContainer { get; set; }
    
            public CookieCollection CookieCollection { get; set; }
    
            public HttpHelp()
            {
                this.CookieCollection = new CookieCollection();
                this.CookieContainer = new CookieContainer();
            }
    
    
            public string GetHtml(string url, string Referer, Encoding encode, bool SaveCookie)
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.Method = "GET";
                req.CookieContainer = this.CookieContainer;
                req.Proxy = null;
                if (!string.IsNullOrEmpty(Referer))
                    req.Referer = Referer;
                using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)
                {
                    if (SaveCookie)
                    {
                        this.CookieCollection = hwr.Cookies;
                        this.CookieContainer.GetCookies(req.RequestUri);
                    }
                    using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))
                    {
                        return SR.ReadToEnd();
                    }
                }
            }
    
            public static string GetHtml(string url, string Referer, Encoding encode)
            {
                return new HttpHelp().GetHtml(url, Referer, encode, false);
            }
    
            public string PostHtml(string url, string Referer, string data, Encoding encode, bool SaveCookie)
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.CookieContainer = this.CookieContainer;
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0";
                req.Proxy = null;
                req.ProtocolVersion = HttpVersion.Version10;
                if (!string.IsNullOrEmpty(Referer))
                    req.Referer = Referer;
                byte[] mybyte = Encoding.Default.GetBytes(data);
                req.ContentLength = mybyte.Length;
                using (Stream stream = req.GetRequestStream())
                {
                    stream.Write(mybyte, 0, mybyte.Length);
                }
                using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)
                {
                    if (SaveCookie)
                    {
                        this.CookieCollection = hwr.Cookies;
                        this.CookieContainer.GetCookies(req.RequestUri);
                    }
                    using (StreamReader SR = new StreamReader(hwr.GetResponseStream(), encode))
                    {
                        return SR.ReadToEnd();
                    }
                }
            }
    
            public Image getImage(string url, string Referer, bool SaveCookie)
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.Method = "GET";
                req.CookieContainer = this.CookieContainer;
                req.Proxy = null;
                if (!string.IsNullOrEmpty(Referer))
                    req.Referer = Referer;
                using (HttpWebResponse hwr = req.GetResponse() as HttpWebResponse)
                {
                    if (SaveCookie)
                    {
                        this.CookieCollection = hwr.Cookies;
                        this.CookieContainer.GetCookies(req.RequestUri);
                    }
                    return Image.FromStream(hwr.GetResponseStream());
                }
            }
        }
  • 相关阅读:
    付费客户端攻防篇
    psql查询结果保存文件
    mysql5.5自定义函数-计算并赋值
    TortoiseSVN 提交时出现错误 Error: Malformed svndiff data in representation
    打开文件共享突然提示:你没有权限访问\。请与网络管理员联系请求访问权限。
    golang 实现笛卡尔积
    爬虫练习3:提取全国省市县信息并统计
    爬虫练习2:爬取省市信息(增加地址信息)
    爬虫练习1:爬取省市信息
    个人使用的效率神器分享【不定期补充】
  • 原文地址:https://www.cnblogs.com/bqh10086/p/4491623.html
Copyright © 2011-2022 走看看