当前位置: 代码迷 >> ASP.NET >> 这种cookie怎么添加到CookieContainer
  详细解决方案

这种cookie怎么添加到CookieContainer

热度:9994   发布时间:2013-02-25 00:00:00.0
这种cookie如何添加到CookieContainer
CZZ_USER_ID=4be00929GFDSFf6854534545998cf6fc76462eed76; DVC_28822=4; DVI_28822=b054b81da46d49a69643657d0a7f62b3da6; DVR_28822; DVV_28822;
字符串型cookie如上,但是我如何添加到CookieContainer???


先用";"来split,然后再用"="来split放到CookieContainer,肯定不行


因为最后2个没有等号


有没有人知道的?


  private CookieContainer GetCookieByWeb()
  {
  CookieContainer DCookieContainer = new CookieContainer();
  string cookieStr =上面的字符串;
  string[] cookstr = cookieStr.Split(';');
  foreach (string str in cookstr)
  {
  string[] cookieNameValue = str.Split('=');
  Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
  ck.Domain = xxxx;
  DCookieContainer.Add(ck);
  }

  return DCookieContainer;
  }

这样做会超出索引数组...

------解决方案--------------------------------------------------------
string str = "cookie内容";
foreach(string s in str.Split(';')){
string name = s.Split('=')[0].Trim();
string value = s.Contains("=")?s.Split('=')[1].Trim():"";
}
  相关解决方案