• 取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
  • 相关阅读:
    socket架构
    异常处理
    类的装饰器
    with&as上下文管理协议
    软件开发规范
    面向对象-描述符
    面向对象-迭代器
    面向对象编程多种特性
    体验Visual Studio 2015 之 MVC
    MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获
  • 原文地址:https://www.cnblogs.com/0x77/p/6589437.html
Copyright © 2020-2023  润新知