zoukankan      html  css  js  c++  java
  • 添加cookie字符串到 CookieContainer 中

     

    补充:

    • webrequest 启用cookietainer 才能使用cookie;但是未启用之前可以在http header 中添加cookier字符串让此次http 有cookie;一旦开启了cookiecontainer的话,则在http  header 添加cookie无效。

     

    • cookiename和cookievalue 不能有=号,不能name相同,cookiename 和value 不能有空格

             格式: cookiename=cookievalue

     

     

     

     

     

    public CookieContainer addCookieToContainer(string cookie,CookieContainer cc,string domian)
          {
              string[] tempCookies = cookie.Split(';');
              string tempCookie = null;
              int Equallength = 0;//  =的位置
              string cookieKey = null;
              string cookieValue = null;
              //qg.gome.com.cn  cookie
              for (int i = 0; i < tempCookies.Length; i++)
              {
                  if (!string.IsNullOrEmpty(tempCookies[i]))
                  {
                      tempCookie = tempCookies[i];

                      Equallength = tempCookie.IndexOf("=");

                      if (Equallength != -1)       //有可能cookie 无=,就直接一个cookiename;比如:a=3;ck;abc=;
                      {

                          cookieKey = tempCookie.Substring(0, Equallength).Trim();
                          //cookie=

                          if (Equallength == tempCookie.Length - 1)    //这种是等号后面无值,如:abc=;
                          {
                              cookieValue = "";
                          }
                          else
                          {
                              cookieValue = tempCookie.Substring(Equallength + 1, tempCookie.Length - Equallength - 1).Trim();
                          }
                      }

                      else
                      {
                          cookieKey = tempCookie.Trim();
                          cookieValue = "";
                      }

                          cc.Add(new Cookie(cookieKey, cookieValue, "", domain));

                  }

              }

              return cc;
          }

  • 相关阅读:
    HDU 5120 A Curious Matt(2014北京赛区现场赛A题 简单模拟)
    HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
    HDU 5120 Intersection(2014北京赛区现场赛I题 计算几何)
    HDU 4793 Collision(2013长沙区域赛现场赛C题)
    HDU 4791 Alice's Print Service(2013长沙区域赛现场赛A题)
    HDU 4803 Poor Warehouse Keeper
    HDU HDU1558 Segment set(并查集+判断线段相交)
    HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)
    HDU 1392 Surround the Trees(凸包*计算几何)
    HDU 1174 爆头(计算几何)
  • 原文地址:https://www.cnblogs.com/StudyLife/p/2592000.html
Copyright © 2011-2022 走看看