|
本帖最后由 echoxxx 于 2021-5-4 01:14 编辑
如下三个从header 获取三个cookie,框架使用cookiecontainor接收只识别出来2个cookie
请修复谢谢{
TGC=eyJhbGciOiJIUzUxMiJ9.ZXlKaGJHY2lPaUprYVhJaUxDSmxibU1pT2lKQk1USTRRMEpETFVoVE1qVTJJbjAuLnZtclpxMlFBTTNRTE96NlozVmltRGcua3RmU2RkbzNERjdrR0Z4cXg2MmdhUUluWFpGMVRMNlR3WXB0VnpuUlRVbFRJR2VmTW92VVZEMkZtSm9PSXFGeVhCcXVFVlotMjlybkRVcDdFYVJsNHcuOXhYVXpoX1BCU1kwZVhzeGVmbEZRZw.Mi5EVtHXc4q9Jo5-CYtdVfYYJsdg48ueZIwCGAkYaJTayv3aJsvlXBhL8U_7lYDeB7H5uo6NiVTHdwM-xSXoqQ; Path=/sso;
HttpOnly,Y1vJ4IdorMglXdNk="lL2TkT3MlsUDyD+TZG1bEzjRhNk3GepyAuwD7glZm/8="; Version=1; Domain=aaa.cn; Path=/; (遗漏)
HttpOnly,SSOExpireTime=1620068391; Domain=aaa.cn; Path=/; HttpOnly
}
奉上在网上找到的方法
public static CookieCollection GetAllCookiesFromHeader(string strHeader, string strHost)
{
if (string.IsNullOrEmpty(strHeader))
{
return null;
}
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.IndexOf("expires=", StringComparison.OrdinalIgnoreCase) > 0)
{
al.Add(strCookTemp + "," + strCookTemp[i + 1]);
i = i + 1;
}
else
{
al.Add(strCookTemp);
}
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.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;
}
|
|