各位好!请教在HttpWebRequest中的二级域名的Cookie发送处理问题。
先来看一看正常的浏览器登录报文情况:HTTP/1.1 200 OK
Server: Tengine/1.5.2
Date: Sun, 22 Dec 2013 13:39:38 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Set-Cookie: passToken=Aq3FBlXT880Y+FyC7LB8rYE0wz8nThC2b740zWzDAUt1YZxxxxeZN2NGEYyOKnShUJfZg7dd7LB/wTuijbC8EjM39u77R0pwRfctT3Ch0C/k51gz9F3fMC3NiKQiJvtfpddKcCltHsZ5Wf65E1EJZQky1RZ+2eO78nFhY3dCRrlTP9zSLhcmzbu72eNpR2U3x2g7LAEfBjvFdWmcbg0xbg==; Domain=account.xiaomi.com; Path=/; HttpOnly
Set-Cookie: passToken=Aq3FBlXT880Y+FyC7LB8rYE0wz8nThC2b740zWzDAUt1YZxxxxeZN2NGEYyOKnShUJfZg7dd7LB/wTuijbC8EjM39u77R0pwRfctT3Ch0C9KrD8kNnrlPDKRh3i7KdIy/9DEzfcT55zi/wtiJmjIJT7o0H17YZZp/R4RZRFSCMI3f7dKeKxHllw6jvLx/Kjrdixiyinbo8LS5rcOWeZD2g==; Domain=account.xiaomi.com; Path=/; HttpOnly
extension-pragma: {"ssecurity":"1dTV1iD5e9LEqQw+qbkrBg==","nonce":6426361413940357120,"psecurity":"pP9Cqr39PFt8eNIyHXLl8w=="}
Set-Cookie: pwdToken=1RmPRcQjADWxFwvTRY4fzsCCqoBrVb0M86ocukF/S9DUvhnPCdOp+wuR26UtCoR8hrwHf4VAFzVdxOhhSMpM8rWQ9eOWVPfDRZeiKc+Namo=; path=/
Set-Cookie: userId=123456789; domain=.xiaomi.com; path=/
Set-Cookie: userId=123456789; domain=account.xiaomi.com; path=/
Set-Cookie: pwdToken=1RmPRcQjADWxFwvTRY4fzsCCqoBrVb0M86ocukF/S9AaDMvgAvKmusbm0+XeQTpB1NsgGHSMKVu+pLZRAukqx7xyys8NZ7Rf280G8b1CvvE=; domain=account.xiaomi.com; path=/
Set-Cookie: userId=123456789; domain=.xiaomi.com; path=/
Set-Cookie: userId=123456789; domain=account.xiaomi.com; path=/
Set-Cookie: pExpireTime=-1; domain=account.xiaomi.com; path=/; secure
Set-Cookie: uLocale=zh_CN; path=/; expires=Mon, 06-Jan-2014 13:39:39 GMT
Cache-Control: no-cache
Content-Length: 806
转到主页:
GET http://www.xiaomi.com/c/service/js/categoryTree.js?ver=2221080 HTTP/1.1
Accept: application/javascript, */*;q=0.8
Referer: http://www.xiaomi.com/
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
DNT: 1
Host: www.xiaomi.com
Cookie: xmuuid=XMGUEST-F7525FF0-6B0B-11E3-BFA4-3F911C8DFC07;
mstuid=1387718489085_3333;
__utma=219621008.581528728.1387718489.1387718489.1387718489.1;
__utmb=219621008.2.10.1387718489;
__utmz=219621008.1387718489.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);
userName=13500000000;
mstz=||1104227302;
__utmc=219621008;
XM_123456789_UN=CatcherX;
passport_slh=2NqVtauh/yAe/JObarLgjekvV4Q=;
userId=123456789
【以下是我在.NET中用HttpWebRequest请求的返回的报文】
/
*
成功登录小米官网后返回的HTTP报文:
HTTP/1.1 200 OK
Server: Tengine/1.5.2
Date: Sun, 22 Dec 2013 13:23:35 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 802
Connection: keep-alive
Set-Cookie: passToken=Aq3FBlXT880Y+FyC7LB8rSOHw9CUUZdChjC2fzQGqMfu4KBbJ0SQaq7KpMpOjhEpjcJy+6pe4Q85PqRJ4j3oGOYAX51n7RVgkfNjYJSYLLZ9uDmekv4OySZDPmpBrGOAkCWnUhY+ffdC4z4lu853XLx8E+74MaW1DWmRGuAltcs7WkqINvqN76MZoSNzilCCzykgTCv8xKrauSh0kX63GA==; Domain=account.xiaomi.com; Path=/; HttpOnly
Set-Cookie: passToken=Aq3FBlXT880Y+FyC7LB8rSOHw9CUUZdChjC2fzQGqMfu4KBbJ0SQaq7KpMpOjhEpjcJy+6pe4Q85PqRJ4j3oGOYAX51n7RVgkfNjYJSYLLbskgVXwlMWBKU3KH79ph1sVu8vGfdN1oX/hZs+0WtTJoa7JBd9iROXiHmU3aoLQMcQ/55So14eiSwKM7kZOn+pYp8bFeOsea17mVQBIyODuA==; Domain=account.xiaomi.com; Path=/; HttpOnly
extension-pragma: {"ssecurity":"Hu2YZFCq8/L7sl7K5IO88g==","nonce":6899786434773787648,"psecurity":"wVm8fvHUXh3ewrRyqjce2g=="}
Set-Cookie: pwdToken=1RmPRcQjADWxFwvTRY4fzsCCqoBrVb0M86ocukF/S9AYmZALdvWPL4S6KbBIhQQ5CSxwFtmKvr62h/OmL5CzwoqDYyqR/8NC4UyarHwudwQ=; path=/
Set-Cookie: userId=123456789; domain=.xiaomi.com; path=/
Set-Cookie: userId=123456789; domain=account.xiaomi.com; path=/
Set-Cookie: pExpireTime=-1; domain=account.xiaomi.com; path=/
Set-Cookie: pwdToken=1RmPRcQjADWxFwvTRY4fzsCCqoBrVb0M86ocukF/S9C3IsBDIIvaHuk/by5CzRa9tfkAF855ZOh2TPNT9BuoGv+h9nGf4x8IkbDUujiIp+0=; domain=account.xiaomi.com; path=/
Set-Cookie: userId=123456789; domain=.xiaomi.com; path=/
Set-Cookie: userId=123456789; domain=account.xiaomi.com; path=/
Set-Cookie: pExpireTime=-1; domain=account.xiaomi.com; path=/; secure
Set-Cookie: uLocale=zh_CN; path=/; expires=Mon, 06-Jan-2014 13:23:35 GMT
Cache-Control: no-cache
*
/
【转到主页】 小米主页:http://www.xiaomi.com/
GET http://www.xiaomi.com/ HTTP/1.1
Accept-Language: zh-CN,zh;q=0.8
Host: www.xiaomi.com
Cookie: userId=123456789
===============================================================================================================
【分析】:成功POST数据到小米官网后,返回的cookie有很多个的域名是: domain=account.xiaomi.com
导致了再次用HttpWebRequest去请求非account.xiaomi.com时,它只会发送这个Cookie:Cookie: userId=123456789
【当我故意设置为相同域名的url时,它就可以发送出去:】如:https://account.xiaomi.com/csdn
GET https://account.xiaomi.com/csdn HTTP/1.1
Accept-Language: zh-CN,zh;q=0.8
Host: account.xiaomi.com
Cookie: uLocale=zh_CN;
JSESSIONID=aaaTv6zWPskV_g_1PRAmu;
deviceId=wb_1928eaa7-0a80-4e80-a796-a877f22b2577;
passToken=Aq3FBlXT880Y+FyC7LB8rR/3RQbQt+a8fTBq+aLpEM1jR4TX/ARFs6RVI/80z+skweRautAeEza1dF1+gOdrKDhVoygfMLR+pfQzw4Yppx1SU1p5cLfCPuYQ4UL0zcWqpASXWj1RblpA4yQ5JxVtF9c1NsWpO1piHrqsR2zIgVPSsait+YASTBtBoFb3B0VtfLnvaooyV9MdcfJsHi+cmQ==;
pwdToken=1RmPRcQjADWxFwvTRY4fzsCCqoBrVb0M86ocukF/S9BqxxqOMAwJ5BwGMON5OgIzc4WIFMao7uh3DgsAtFmAWdlxNhri/uVYBoYbAoIvQya4=;
userId=123456789;
pExpireTime=-1;
userId=123456789
【问题】要怎样处理可以像浏览器那样把cookie再传过去呢?
我现在的Cookie是这样处理的:
public static CookieContainer _cookieContainer = new CookieContainer();--------------------编程问答-------------------- 【参考文章】:
.......
/// <summary>
/// 解析Cookie
/// </summary>
/// <param name="response"></param>
private void ParseCookie(HttpWebResponse response)
{
if (!IgnoreCookies)
{
string cookiesExpression = response.Headers["Set-Cookie"];
if (!string.IsNullOrEmpty(cookiesExpression))
{
Uri cookieUrl = new Uri(string.Format("{0}://{1}:{2}/",
response.ResponseUri.Scheme,
response.ResponseUri.Host,
response.ResponseUri.Port));
try
{
_cookieContainer.SetCookies(cookieUrl, cookiesExpression);
}
catch
{ }
}
}
}
/// <summary>
/// 配置请求设置
/// </summary>
/// <param name="request"></param>
private void ConfigRequest(HttpWebRequest request)
{
.....
request.CookieContainer = _cookieContainer;
....
}
HttpWebRequest跨域cookie的访问 - 芽芽民工广场 - 博客频道 - CSDN.NET
http://blog.csdn.net/etstudio/article/details/1897071
补充:.NET技术 , ASP.NET