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());
                }
            }
        }
  • 相关阅读:
    python切片操作
    python中的内存管理
    python中x,y交换值的问题
    leetcode6:Zigzag Conversion@Python
    Leetcode4:Median of Two Sorted Arrays@Python
    Leetcode3:Longest Substring Without Repeating Characters@Python
    Leetcode2:Add Two Numbers@Python
    LeetCode344:Reverse String@Python
    支付宝 芝麻信用分过600,你不知道的八个特权
    穷爸爸富爸爸里面说的“现金流游戏”靠谱吗?
  • 原文地址:https://www.cnblogs.com/bqh10086/p/4491623.html
Copyright © 2011-2022 走看看