C#解析Cookie字符串为CookieCollection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 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; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2017-10-26 社会学
2012-10-26 .NET简谈事务、分布式事务处理
2012-10-26 .NET简谈事务本质论