C#解析Cookie字符串为CookieCollection
来源:互联网 发布:php去掉所有html标签 编辑:程序博客网 时间:2024/06/04 20:14
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格式字符串
- 编写Makefile规则
- [Android新手学习笔记32]-Broadcast-强制下线
- 假期里入门JS的一些感受(一个小坑)
- ListView嵌套GridView出现的问题及解决方法
- 【BZOJ 1800】【AHOI 2009】飞行棋
- C#解析Cookie字符串为CookieCollection
- [Android新手学习笔记33]-Storage-文件存储
- Java中的局部变量表及使用jclasslib进行查看
- [Android新手学习笔记34]-Storage-SharedPreferences
- 【解题报告】Codeforces Round #395 (Div. 2)
- [Android新手学习笔记35]-Storage-SQLite
- Android在图片画线(放大,旋转,平移后可继续正确画线,限制画线区域)
- hdu1398(完全背包)
- 谨慎能捕千秋蝉(三)——界面操作劫持与HTML5安全