取Cookie值

            string url_Login_Group = "http://ui.ptlogin2.qq.com/cgi-bin/login?appid=549000912&daid=5&style=12&s_url=http%3A%2F%2Fqun.qzone.qq.com%2Fgroup%23!%2F336100817%2Fmember";
            string WebCode = GetWebString(url_Login_Group);
            //获取Cookie值
            //string qrsig = Cookies.GetCookies(new Uri("http://ptlogin2.qq.com"))["qrsig"].Value;
            //获取Cookie值
            string uikey = Cookies.GetCookies(new Uri("https://xui.ptlogin2.qq.com"))["uikey"].Value;
            Console.WriteLine(uikey);
            Console.WriteLine(WebCode);
            Console.ReadLine();

 

public static CookieContainer Cookies = new CookieContainer();
        public static string GetWebString(string url, string referer = null, Encoding encode = null)
        {
            string webdat = null;
            HttpWebRequest req = null;
            HttpWebResponse res = null;
            try
            {
                req = (HttpWebRequest)WebRequest.Create(url);
                req.CookieContainer = Cookies;
                req.AllowAutoRedirect = false;
                req.Timeout = 10000;
                req.Referer = referer;
                req.Proxy = null;
                req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
                res = (HttpWebResponse)req.GetResponse();
                Cookies.Add(res.Cookies);
                using (StreamReader reader = new StreamReader(res.GetResponseStream(), encode == null ? Encoding.UTF8 : encode))
                {
                    webdat = reader.ReadToEnd();
                }
                //using (FileStream fs = new FileStream("webdat.txt", FileMode.Create))
                //{
                //    byte[] data = Encoding.UTF8.GetBytes(webdat);
                //    fs.Write(data, 0, data.Length);
                //}
            }
            catch (HttpException httpex)
            {
                return "HttpException:" + httpex + "";
            }
            catch (WebException webex)
            {
                return "WebException:" + webex + "";
            }
            finally
            {
                if (res != null)
                {
                    res.Close();
                }
                if (req != null)
                {
                    req.Abort();
                }
            }
            return webdat;
        }
View Code

 

你可能感兴趣的:(取Cookie值)