C#解析Cookie字符串为CookieCollection
来源:互联网 发布:西岐网络新三板 编辑:程序博客网 时间:2024/05/16 07:04
public static CookieCollection GetAllCookiesFromHeader(string strHeader, string strHost) { ArrayList al = new ArrayList(); CookieCollection cc = new CookieCollection(); if (strHeader != string.Empty) { al = ConvertCookieHeaderToArrayList(strHeader); cc = ConvertCookieArraysToCookieCollection(al, strHost); } return cc; } private static ArrayList ConvertCookieHeaderToArrayList(string strCookHeader) { strCookHeader = strCookHeader.Replace("\r", ""); strCookHeader = strCookHeader.Replace("\n", ""); string[] strCookTemp = strCookHeader.Split(','); ArrayList al = new ArrayList(); int i = 0; int n = strCookTemp.Length; while (i < n) { if (strCookTemp[i].IndexOf("expires=", StringComparison.OrdinalIgnoreCase) > 0) { al.Add(strCookTemp[i] + "," + strCookTemp[i + 1]); i = i + 1; } else { al.Add(strCookTemp[i]); } i = i + 1; } return al; } private static CookieCollection ConvertCookieArraysToCookieCollection(ArrayList al, string strHost) { CookieCollection cc = new CookieCollection(); int alcount = al.Count; string strEachCook; string[] strEachCookParts; for (int i = 0; i < alcount; i++) { strEachCook = al[i].ToString(); strEachCookParts = strEachCook.Split(';'); int intEachCookPartsCount = strEachCookParts.Length; string strCNameAndCValue = string.Empty; string strPNameAndPValue = string.Empty; string strDNameAndDValue = string.Empty; string[] NameValuePairTemp; Cookie cookTemp = new Cookie(); for (int j = 0; j < intEachCookPartsCount; j++) { if (j == 0) { strCNameAndCValue = strEachCookParts[j]; if (strCNameAndCValue != string.Empty) { int firstEqual = strCNameAndCValue.IndexOf("="); string firstName = strCNameAndCValue.Substring(0, firstEqual); string allValue = strCNameAndCValue.Substring(firstEqual + 1, strCNameAndCValue.Length - (firstEqual + 1)); cookTemp.Name = firstName; cookTemp.Value = allValue; } continue; } if (strEachCookParts[j].IndexOf("path", StringComparison.OrdinalIgnoreCase) >= 0) { strPNameAndPValue = strEachCookParts[j]; if (strPNameAndPValue != string.Empty) { NameValuePairTemp = strPNameAndPValue.Split('='); if (NameValuePairTemp[1] != string.Empty) { cookTemp.Path = NameValuePairTemp[1]; } else { cookTemp.Path = "/"; } } continue; } if (strEachCookParts[j].IndexOf("domain", StringComparison.OrdinalIgnoreCase) >= 0) { strPNameAndPValue = strEachCookParts[j]; if (strPNameAndPValue != string.Empty) { NameValuePairTemp = strPNameAndPValue.Split('='); if (NameValuePairTemp[1] != string.Empty) { cookTemp.Domain = NameValuePairTemp[1]; } else { cookTemp.Domain = strHost; } } continue; } } if (cookTemp.Path == string.Empty) { cookTemp.Path = "/"; } if (cookTemp.Domain == string.Empty) { cookTemp.Domain = strHost; } cc.Add(cookTemp); } return cc; }
0 0
- C#解析Cookie字符串为CookieCollection
- C#解析Cookie字符串为CookieCollection
- C#一个到多个Cookie的字符串添加到CookieCollection集合中【isGood代码】
- CookieCollection
- js操作cookie,将字符串数据解析为json格式
- C# 把字符串解析为数据值
- 字符串解析为整数
- C#解析XML字符串
- C#通用字符串解析
- C#解析XML字符串
- C# 解析JSON字符串
- c#后台将数据转换为json字符串,前台ajax解析json字符串
- C#解析json为ArrayList
- C#判断字符串为数字
- C# 字符串为空判断
- C#判断字符串为空
- 字符串表达式解析(C#源码)
- C# 解析json格式字符串
- 使用jsp+javaBean写一个网页计算器
- 新的征途
- 【转】mysql查看日志位置和开启日志
- ThinkPad无法安装系统的解决办法
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- C#解析Cookie字符串为CookieCollection
- mysql in 子查询 效率慢 优化
- c#下实现ping操作【转】
- 1010. 一元多项式求导 (25)
- 用Java来输出soap报文
- c# 通过快捷方式读取目标路径
- dwz.js表格自动适应宽度
- Windows安装Apache 2.2.17、PHP 5.3.5与MySQL
- Activiti博客资源贴