• c# HttpWebRequest Cookie 设置到 webBrowser 控件


       [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
    
    
            void Form3_Load(object sender, EventArgs e)
            {
                 InternetSetCookie("http://localhost/1.asp",null,Common.cookie );
                // webBrowser1.Navigate("http://localhost/1.asp");
                webBrowser1.Navigate("http://localhost/1.asp");
            
           
            }
    
    
    //get cookie from HttpWebRequest
    
      string s=  HttpHelper.httpGet("http://localhost/login.asp");
              string cookie= Common.cookie=  HttpHelper.myCookieContainer.GetCookieHeader(new Uri("http://localhost/"));
              MessageBox.Show(cookie);
    
    
    
     public static class HttpHelper
        {
    
            public static CookieContainer myCookieContainer = new CookieContainer();
    
            public static void initCookie()
            {
                myCookieContainer = new CookieContainer();
                string cookieStr = "accessID=20170817112826535103; tempID=4007780019; OnceLoginWEB=91919420; lastLoginDate=Thu%20Sep%2020%202018%2014%3A27%3A55%20GMT+0800%20%28China%20Standard%20Time%29; orderSource=10130301; NTKF_T2D_CLIENTID=guestFE5FC01D-72B3-4E96-7D6F-D08ACEE9E294; Hm_lvt_5caa30e0c191a1c525d4a6487bf45a9d=1537424876; Hm_lpvt_5caa30e0c191a1c525d4a6487bf45a9d=1537425679; Hm_lvt_8bcb99d7b6c2fba606d7089f6310d8bc=1537425835; Hm_lpvt_8bcb99d7b6c2fba606d7089f6310d8bc=1537427219; accessToken=baihe-1537427221699-e5a06bb78317b; _fmdata=9YMB%2BufU%2Fi4Gm2L72UeCAckBNQ5bzOuzmv9VtK7zki05zclzrqEhzWoPzAi7UJki9ekiKy4WxkI%2FWQ%2FuZlEqN9vHyrZ%2F9Nh5olgJUqhyjEY%3D";
                string[] cookstr = cookieStr.Split(";".ToCharArray());
                foreach (string str in cookstr)
                {
                    string[] cookieNameValue = str.Split('=');
                    Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                    ck.Domain = "localhost";//必须写对 
                    myCookieContainer.Add(ck);
                }
    
            }
    
    
            public static string httpGet(string url)
            {
                string s = "";
                try
                {
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                    req.CookieContainer = myCookieContainer;
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                   // Common.cookie = resp.Headers["Set-Cookie"];
                    //MessageBox.Show(resp.Headers["Set-Cookie"]);
                    myCookieContainer.Add(resp.Cookies);
                    StreamReader r = new StreamReader(resp.GetResponseStream());
                    s = r.ReadToEnd();
                    r.Close();
                    resp.Close();
                    req.Abort();
    
                }
                catch (Exception ex)
                {
    
                    s = ex.Message;
    
                }
    
    
                return s;
            }
    
    
            public static string httpPost(string url, string post)
            {
                string s = "";
                try
                {
                    byte[] data = Encoding.ASCII.GetBytes(post);
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                    req.Method = "POST";
                    req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
                    req.ContentLength = data.Length;
                    req.GetRequestStream().Write(data, 0, data.Length);
                    req.CookieContainer = myCookieContainer;
                    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                    myCookieContainer.Add(resp.Cookies);
                    StreamReader r = new StreamReader(resp.GetResponseStream());
                    s = r.ReadToEnd();
                    r.Close();
                    resp.Close();
                    req.Abort();
    
                }
                catch (Exception ex)
                {
    
                    s = ex.Message;
    
                }
    
    
                return s;
            }
    
    
        }
    

      

  • 相关阅读:
    [leetcode] LRU Cache @ Python
    [leetcode]Swap Nodes in Pairs @ Python
    [leetcode]Add Two Numbers @ Python
    [leetcode]Candy @ Python
    [leetcode]Gas Station @ Python
    [leetcode]Plus One @ Python
    接口测试-压力-Jmeter继续使用
    移动互联网应用测试,推荐两本书
    Android dumpsys 内存分析
    内存泄露分析之MAT工具简单使用
  • 原文地址:https://www.cnblogs.com/wgscd/p/9686957.html
Copyright © 2020-2023  润新知