CookieContainer
複数のCookieを受け取って保存したい。サーバーは、1つのリクエスト中にこのコードを私に送信します(この行がFiddlerに表示されます)。
Set-Cookie:ASP.NET_SessionId = fjkfjrfjeijrfjrifj; パス=/; HttpOnly
Set-Cookie:rua-usm = date = 10.09.2012&ptype = 1&ts = 11&id = cvvkmrfmpr44r4rjj; 有効期限=金、31-Dec-9999 23:59:59 GMT; パス=/
Set-Cookie:rua-usm2 = date = 10.09.2012&ptype = 1&ts = 11; 有効期限=金、31-Dec-9999 23:59:59 GMT; パス=/
Set-Cookie:VisitorDone = 1; 有効期限=金、31-Dec-9999 23:59:59 GMT; パス=/
ご覧のとおり、4つのCookieがあります。しかし、私が読んだとき、私はresponse.Cookies.Count
1つだけを取得します:*ASP.NET_SessionId*。
次のコードを使用します:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some site");
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Set-cookieフィールドを手動で解析してみました:
string cookiesText = (response as HttpWebResponse).Headers[HttpResponseHeader.SetCookie];
if (!String.IsNullOrEmpty(cookiesText))
{
CookieCollection cookiesList = GetAllCookiesFromHeader(cookiesText, host);
if (cookiesList != null)
_cookieContainer.Add(new Uri(host),cookiesList);
}
private 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 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 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;
Encoding iso = Encoding.GetEncoding("utf-8");//may be utf-8
allValue = HttpUtility.UrlEncode(allValue, iso);
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;
}
しかし、エラーが発生しました:
「Cookieの「ドメイン」=「http://xxxx.xxxx」の部分が無効です。」
私が理解しているように、それはSet-Cookie値への標準ではない文字に関連しています。HttpUtility.UrlEncode
しかし、私はすべての値を!でエンコードしました。