reg.163.com
五、附录
附录一 163 登录时请求和发送数据包
(1) GET
http://adgeo.163.com/ad_cookies
请求头部
GET /ad_cookies HTTP/1.1
Accept: */*
Accept-Language: zh-cn,en-US;q=0.5
UA-cpu: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; windows NT 5.1; myie2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; CIBA)
Host: adgeo.163.com
Connection: Keep-Alive
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
应答头部
HTTP/1.1 200 OK
Server: nginx/<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />
Date: Tue, 02 Dec 2008 06:40:45 GMT
content-Type: text/plain
Content-Length: 0
Connection: close
cache-Control: no-cache
P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Set-Cookie: Province=025; expires=Tue, 16 Dec 2008 06:40:45 GMT; Path=/; Domain=.163.com
(2) POST
https://reg.163.com/logins.jsp?type=1&url=http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1
请求头部
POST /logins.jsp?type=1&url=http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1 HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, APPlication/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/x-shockwave-flash, */*
Referer: http://mail.163.com/
Accept-Language: zh-cn,en-US;q=0.5
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MyIE2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; CIBA)
Host: reg.163.com
Content-Length: 131
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: Province=025; ntes_mail_firstpage=normal
请求体
verifycookie=1&style=-1&product=mail163&username=xjtujay&password=jinjian&selType=-1&remUser=on&secure=on&%B5%C7%C2%BC%D3%CA%CF%E4=
应答头部
HTTP/1.1 200 OK
Date: Tue, 02 Dec 2008 06:40:56 GMT
Server: Apache
Cache-Control: max-age=0
Expires: Tue, 02 Dec 2008 06:40:56 GMT
pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Length: 2575
Set-Cookie: NTES_SESS=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AMD5YMQLzsQ1CjkLkdLSsVF; domain=.163.com; path=/
Set-Cookie: NETEASE_SSN=xjtujay; domain=.163.com; path=/; expires=Wed, 02-Dec-2009 06:40:54 GMT
Set-Cookie: NETEASE_ADV=11&23&1228200054655; domain=.163.com; path=/; expires=Wed, 02-Dec-2009 06:40:54 GMT
Set-Cookie: URSJESSIONID=aGHZvw0enBrb; path=/
Connection: close
Content-Type: text/html; charset=UTF-8
应答内容
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>跳转提示</title>
<style type="text/css">
<!--
*{margin:0;padding:0;}
body{margin:50px;background:#fff;line-height:130%;}
p{width:560px;border:5px solid #f
h1{margin:0 0 10px;padding-bottom:10px;border-bottom:1px solid #bbb;font-size:16px;color:blue;font-weight:normal;}
h2{font-size:18px;line-height:150%;}
p{font-size:14px;}
img{margin:10px 0;}
a{color:blue;}
a:hover{color:red;}
-->
</style>
<script language="javaScript">
window.location.replace("http://reg.163.com/crossdomain.jsp?username=xjtujay&loginCookie=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF&url=http%
</script>
<META HTTP-EQUIV=REFRESH CONTENT="0;URL=http://reg.163.com/crossdomain.jsp?username=xjtujay&loginCookie=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF&url=http%
</head>
<body>
<p id="login_hint" style="visibility : hidden">正在登录...请稍等</p>
<p id="block_hint" style="visibility : hidden">
<h1>如果您看到这个页面,说明您的网速缓慢或者浏览器阻止您在https和http页面间跳转。<br />
请您点击<a href="http://reg.163.com/crossdomain.jsp?username=xjtujay&loginCookie=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF&url=http%
<h2>温馨提示:</h2>
<p>从HTTPS页面跳转到HTTP会出现安全提示对话框,这是浏览器本身的设置造成的。如果不希望每次访问HTTPS的站点都有这样的提示,在出现安全提示时请您点“是”,并勾中"以后不再显示该警告"即可。</p>
</p>
<script language="JavaScript">
<!--
settimeout( function(){
document.getelementbyid("login_hint").style.visibility = 'visible';
}, 3000);
setTimeout( function(){
document.getElementById("login_hint").style.visibility = 'hidden';
document.getElementById("block_hint").style.visibility = 'visible';
}, 10000);
// -->
</script>
</body>
</html>
(3) GET
http://reg.163.com/crossdomain.jsp?username=xjtujay&loginCookie=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF&url=http%3A%2F%2Ffm163.163.com%2Fcoremail%2Ffcg%2Fntesdoor2%3Flightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1%26username%3Dxjtujay
请求头部
GET /crossdomain.jsp?username=xjtujay&loginCookie=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF&url=http%
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/x-shockwave-flash, */*
Accept-Language: zh-cn,en-US;q=0.5
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MyIE2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; CIBA)
Host: reg.163.com
Connection: Keep-Alive
Cookie: Province=025; ntes_mail_firstpage=normal; NTES_SESS=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF; NETEASE_SSN=xjtujay; NETEASE_ADV=11&23&1228200054655; URSJESSIONID=aGHZvw0enBrb
应答头部
HTTP/1.1 200 OK
Date: Tue, 02 Dec 2008 06:40:52 GMT
Server: Apache
Cache-Control: max-age=0
Expires: Tue, 02 Dec 2008 06:40:52 GMT
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Length: 2145
Connection: close
Content-Type: text/html; charset=UTF-8
应答内容
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="http://reg.yodao.com/setcookie.jsp?username=xjtujay&loginCookie=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF&domain=yodao.com" type="text/css" />
<link rel="stylesheet" href="http://reg.youdao.com/setcookie.jsp?username=xjtujay&loginCookie=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF&domain=youdao.com" type="text/css" />
<title>跳转提示</title>
<style type="text/css">
<!--
*{margin:0;padding:0;}
body{margin:50px;background:#fff;line-height:130%;}
p{width:560px;border:5px solid #f
h1{margin:0 0 10px;padding-bottom:10px;border-bottom:1px solid #bbb;font-size:16px;color:blue;font-weight:normal;}
h2{font-size:18px;line-height:150%;}
p{font-size:14px;}
img{margin:10px 0;}
a{color:blue;}
a:hover{color:red;}
-->
</style>
<script language="JavaScript">
window.location.replace("http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=xjtujay");//remain for popo ,don't
</script>
<
</head>
<body>
<p id="login_hint" style="visibility : hidden">正在登录...请稍等</p>
<p id="block_hint" style="visibility : hidden">
<h1>如果您看到这个页面,说明网速缓慢或者浏览器阻止了您的页面转向。请您点击<a href="http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=xjtujay"><strong><font color=red>这里</font></strong></a>继续。</h1>
</p>
<script language="JavaScript">
<!--
setTimeout( function(){
document.getElementById("login_hint").style.visibility = 'visible';
}, 3000);
setTimeout( function(){
document.getElementById("login_hint").style.visibility = 'hidden';
document.getElementById("block_hint").style.visibility = 'visible';
}, 10000);
// -->
</script>
</body>
</html>
(4) GET
http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=xjtujay
请求头部
GET /coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=xjtujay HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/x-shockwave-flash, */*
Accept-Language: zh-cn,en-US;q=0.5
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MyIE2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; CIBA)
Host: fm163.163.com
Connection: Keep-Alive
Cookie: Province=025; ntes_mail_firstpage=normal; NTES_SESS=NYQeUfIIQZhT16_9RKAAz36JIgKh2aGz7IeDK8H.pFklFNEUgF1EKWkKAgAhPL.F30AmD5YMQLzsQ1CjkLkdLSsVF; NETEASE_SSN=xjtujay; NETEASE_ADV=11&23&1228200054655
应答头部
HTTP/1.1 302 Found
Date: Tue, 02 Dec 2008 06:40:56 GMT
Server: Apache
Set-Cookie: Coremail=TYlruaxjaLMaE%TBrNkFKKhBjsxfeRvrKKnDlwogztEEjg; path=/; domain=163.com
Location: http://tg
Content-Length: 264
Connection: close
Content-Type: text/html; charset=iso-8859-1
应答内容
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://tg
</body></html>
附录二 通讯录示例HTML
<!-- address/address.htm 通讯录 联系人-->
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="JavaScript" src="http://mimg.163.com/jsstyle/js/samedomain.js"></script>
<script language="JavaScript">
<!--
function fLoadData(){
var jsFrame = window.parent.parent.frames["jsFrame"];
jsFrame.CM[ name ] = obj;
jsFrame.MM.createModule( name );
}
var name= "address";
var obj = new Object();
obj.page= new Array();
obj.row = new Array();
obj.a = new Array();
obj.a["add"] = "/coremail/fcg/ldvcapp?funcid=loadiadd&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&ifirstv=&showlist=all&add.x=1";
obj.a["sortN"] = "/coremail/fcg/ldvcapp?funcid=prtsearchres&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&sortattr_N=&showlist=all&ifirstv=";
obj.a["sortE"] = "/coremail/fcg/ldvcapp?funcid=prtsearchres&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&sortattr_EMAIL;PREF=&showlist=all&ifirstv=";
obj.a["find"] = "/coremail/fcg/ldvcapp?funcid=prtsearchres&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&tempname=address/address.htm&showlist=all&ifirstv=";
obj.img = new Array();
obj.img["sortN"]= '';
obj.img["sortE"]= '';
var i , xmp;
//-->
</script>
</head><body>
<form name="previnfo">
<input type="hidden" name="funcid" value="address" >
<input type="hidden" name="sid" value="XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK" >
<input type="hidden" name="tempname" value="address/address.htm" >
<input type="hidden" name="ifirstv" value="" >
<input type="hidden" name="listnum" value="0" >
<input type="hidden" name="showlist" value="all" ></form>
<xmp id="hide_form">
<input type="hidden" name="sid" value="XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK" >
<input type="hidden" name="ifirstv" value="">
</xmp>
<xmp id="xmpListnum">
<select name="listnum">
<option value="0" selected >不分页</option>
<option value="10" >每页10项</option>
<option value="20" >每页20项</option>
<option value="50" >每页50项</option>
<option value="100" >每页100项</option>
</select>
</xmp>
<xmp id="xmpShowlist">
<select name="showlist">
<option value="all" selected >全部分组</option>
<option value="FRIENDS">朋友 </option><option value="FAMILY">亲人 </option><option value="COWORKERS">同事 </option><option value="NETFRIENDS">网友 </option><option value="recent">常用联系人 </option>
</select>
</xmp>
<xmp id="xmpGname">
<select name="gname">
<option value="FRIENDS">朋友 </option><option value="FAMILY">亲人 </option><option value="COWORKERS">同事 </option><option value="NETFRIENDS">网友 </option><option value="RECENT">常用联系人 </option>
</select>
</xmp>
<xmp id="t 1">张旭<space>[email protected]</xmp>
<script language="JavaScript">
xmp = document.getElementById("t 1");
xmp = xmp.innerHTML.split("<space>");
i = 1 - 1;
obj.row[i] = new Object();
obj.row[i].chkname = "chk-_0_7_";
obj.row[i].chkvalue = "0064235%40sina.com";
obj.row[i].iname = xmp[0];
obj.row[i].iemail = xmp[1];
obj.row[i].asend = "/coremail/fcg/ldmmapp?sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&funcid=compose&to=%22%D5%C5%D0%F1%22%20%
obj.row[i].afind = "/coremail/fcg/ldsrchapp?funcid=srchhand&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&word=0064235%40sina.com&fromonly=yes&fid=0&subfolder=yes&perfectmatch=1&rtnurl=%2Fcoremail%2Ffcg%2Fldvcapp%3Ffuncid%3Daddress%26sid%3DXApcTnKKGzpsqgDHliKKgiYNjfXyQlnK%26tempname%3Daddress%252Faddress.htm%26ifirstv%3D%26listnum%3D0%26showlist%3Dall%26total%3D26";
obj.row[i].ainfo = "/coremail/fcg/ldvcapp?funcid=prtsearchres&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&iid=7&mode=1&emptymode=1&print.x=1&tempname=address/add.htm";
obj.row[i].aedit = "/coremail/fcg/ldvcapp?funcid=loadiadd&iid=7&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&ifirstv=&lid=&modify.x=1";
</script>
<xmp id="t 2">颜克斌<space>[email protected]</xmp>
<script language="JavaScript">
xmp = document.getElementById("t 2");
xmp = xmp.innerHTML.split("<space>");
i = 2 - 1;
obj.row[i] = new Object();
obj.row[i].chkname = "chk-_0_11_";
obj.row[i].chkvalue = "yankebin%40163.com";
obj.row[i].iname = xmp[0];
obj.row[i].iemail = xmp[1];
obj.row[i].asend = "/coremail/fcg/ldmmapp?sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&funcid=compose&to=%22%D1%D5%BF%CB%B1%F3%22%20%3Cyankebin%40163.com%3E";
obj.row[i].afind = "/coremail/fcg/ldsrchapp?funcid=srchhand&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&word=yankebin%40163.com&fromonly=yes&fid=0&subfolder=yes&perfectmatch=1&rtnurl=%2Fcoremail%2Ffcg%2Fldvcapp%3Ffuncid%3Daddress%26sid%3DXApcTnKKGzpsqgDHliKKgiYNjfXyQlnK%26tempname%3Daddress%252Faddress.htm%26ifirstv%3D%26listnum%3D0%26showlist%3Dall%26total%3D26";
obj.row[i].ainfo = "/coremail/fcg/ldvcapp?funcid=prtsearchres&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&iid=11&mode=1&emptymode=1&print.x=1&tempname=address/add.htm";
obj.row[i].aedit = "/coremail/fcg/ldvcapp?funcid=loadiadd&iid=11&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&ifirstv=&lid=&modify.x=1";
</script>
<xmp id="t 3">abuse<space>[email protected]</xmp>
<script language="JavaScript">
xmp = document.getElementById("t 3");
xmp = xmp.innerHTML.split("<space>");
i = 3- 1;
obj.row[i] = new Object();
obj.row[i].chkname = "chk-_0_17_";
obj.row[i].chkvalue = "abuse%40service.netease.com";
obj.row[i].iname = xmp[0];
obj.row[i].iemail = xmp[1];
obj.row[i].asend = "/coremail/fcg/ldmmapp?sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&funcid=compose&to=%22abuse%22%20%3Cabuse%40service.netease.com%3E";
obj.row[i].afind = "/coremail/fcg/ldsrchapp?funcid=srchhand&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&word=abuse%40service.netease.com&fromonly=yes&fid=0&subfolder=yes&perfectmatch=1&rtnurl=%2Fcoremail%2Ffcg%2Fldvcapp%3Ffuncid%3Daddress%26sid%3DXApcTnKKGzpsqgDHliKKgiYNjfXyQlnK%26tempname%3Daddress%252Faddress.htm%26ifirstv%3D%26listnum%3D0%26showlist%3Dall%26total%3D26";
obj.row[i].ainfo = "/coremail/fcg/ldvcapp?funcid=prtsearchres&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&iid=17&mode=1&emptymode=1&print.x=1&tempname=address/add.htm";
obj.row[i].aedit = "/coremail/fcg/ldvcapp?funcid=loadiadd&iid=17&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&ifirstv=&lid=&modify.x=1";
</script>
<xmp id="t 19">peter<space>[email protected]</xmp>
<script language="JavaScript">
xmp = document.getElementById("t 19");
xmp = xmp.innerHTML.split("<space>");
i = 19 - 1;
obj.row[i] = new Object();
obj.row[i].chkname = "chk-_0_18_";
obj.row[i].chkvalue = "peter%40mail.xjtu.edu.cn";
obj.row[i].iname = xmp[0];
obj.row[i].iemail = xmp[1];
obj.row[i].asend = "/coremail/fcg/ldmmapp?sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&funcid=compose&to=%22peter%22%20%3Cpeter%40mail.xjtu.edu.cn%3E";
obj.row[i].afind = "/coremail/fcg/ldsrchapp?funcid=srchhand&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&word=peter%40mail.xjtu.edu.cn&fromonly=yes&fid=0&subfolder=yes&perfectmatch=1&rtnurl=%2Fcoremail%2Ffcg%2Fldvcapp%3Ffuncid%3Daddress%26sid%3DXApcTnKKGzpsqgDHliKKgiYNjfXyQlnK%26tempname%3Daddress%252Faddress.htm%26ifirstv%3D%26listnum%3D0%26showlist%3Dall%26total%3D26";
obj.row[i].ainfo = "/coremail/fcg/ldvcapp?funcid=prtsearchres&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&iid=18&mode=1&emptymode=1&print.x=1&tempname=address/add.htm";
obj.row[i].aedit = "/coremail/fcg/ldvcapp?funcid=loadiadd&iid=18&sid=XApcTnKKGzpsqgDHliKKgiYNjfXyQlnK&ifirstv=&lid=&modify.x=1";
</script>
<script language="JavaScript">
<!--
obj.hide_form = document.getElementById("hide_form").innerHTML;
obj.listnum = document.getElementById("xmpListnum").innerHTML;
obj.showlist = document.getElementById("xmpShowlist").innerHTML;
obj.gname = document.getElementById("xmpGname").innerHTML;
obj.previnfo = document.forms["previnfo"];
fLoadData();
//-->
</script>
</body></html>
<!-- CoreMail Version 3.1_dev Copyright (c) 2002-2008 www.mailtech.cn -->
附录三 Cookie以及Cookiehandler的实现
Cookie的实现
import java.net.*;
import java.text.*;
import java.util.*;
public class Cookie {
String name;
String value;
URI uri;
String domain;
Date expires;
String path;
private static DateFormat expiresFormat1
= new simpledateformat("E, dd MMM yyyy k:m:s 'GMT'", locale.US);
private static DateFormat expiresFormat2
= new SimpleDateFormat("E, dd-MMM-yyyy k:m:s 'GMT'", Locale.US);
/**
* Construct a cookie from the URI and header fields
*
* @param uri URI for cookie
* @param header Set of attributes in header
*/
public Cookie(URI uri, String header) {
String attributes[] = header.split(";");
String nameValue = attributes[0].trim();
this.uri = uri;
this.name =
nameValue.substring(0, nameValue.indexof('='));
this.value =
nameValue.substring(nameValue.indexOf('=')+1);
this.path = "/";
this.domain = uri.getHost();
for (int i=1; i < attributes.length; i++) {
nameValue = attributes[i].trim();
int equals = nameValue.indexOf('=');
if (equals == -1) {
continue;
}
String name = nameValue.substring(0, equals);
String value = nameValue.substring(equals+1);
if (name.equalsignorecase("domain")) {
String uriDomain = uri.getHost();
if (uriDomain.equals(value)) {
this.domain = value;
} else {
if (!value.startsWith(".")) {
value = "." + value;
}
uriDomain = uriDomain.substring(
uriDomain.indexOf('.'));
if (!uriDomain.equals(value)) {
throw new illegalargumentException(
"Trying to set foreign cookie");
}
this.domain = value;
}
} else if (name.equalsIgnoreCase("path")) {
this.path = value;
} else if (name.equalsIgnoreCase("expires")) {
try {
this.expires = expiresFormat1.parse(value);
} catch (ParseException e) {
try {
this.expires = expiresFormat2.parse(value);
} catch (ParseException e2) {
throw new IllegalArgumentException(
"Bad date format in header: " + value);
}
}
}
}
}
public boolean hasExpired() {
if (expires == null) {
}
Date now = new Date();
return now.after(expires);
}
public String getName() {
return name;
}
public URI getURI() {
return uri;
}
/**
* Check if cookie isn't expired and if URI matches,
* should cookie be included in response.
*
* @param uri URI to check against
* @return true if match, false otherwise
*/
public boolean matches(URI uri) {
if (hasExpired()) {
return false;
}
String path = uri.getPath();
if (path == null) {
path = "/";
}
return path.startsWith(this.path);
}
public String toString() {
stringbuilder result = new StringBuilder(name);
result.append("=");
result.append(value);
return result.toString();
}
}
CookieHandler的实现
import java.io.*;
import java.net.*;
import java.util.*;
public class ListCookieHandler extends CookieHandler {
// "Long" term storage for cookies, not serialized so only
// for current JVM instance
private List<Cookie> cache = new LinkedList<Cookie>();
/**
* Saves all applicable cookies present in the response headers into cache.
*
* @param uri
* URI source of cookies
* @param responseHeaders
* Immutable map from field names to lists of field values
* representing the response header fields returned
*/
public void put(URI uri, Map<String, List<String>> responseHeaders)
throws IOException {
System.out.println("Cache: " + cache);
List<String> setCookieList = responseHeaders.get("Set-Cookie");
if (setCookieList != null) {
for (String item : setCookieList) {
Cookie cookie = new Cookie(uri, item);
// Remove cookie if it already exists
// New one will replace
for (Cookie existingCookie : cache) {
if ((cookie.getURI().equals(existingCookie.getURI()))
&& (cookie.getName().equals(existingCookie
.getName()))) {
cache.remove(existingCookie);
break;
}
}
System.out.println("Adding to cache: " + cookie);
cache.add(cookie);
}
}
}
public void print() {
System.out.println("Cache: " + cache);
}
public void remove(String name) {
for (Cookie existingCookie : cache) {
if (name.equals(existingCookie.getName())) {
System.out.println("Remove from cache: " + existingCookie);
cache.remove(existingCookie);
break;
}
}
}
public void put(URI uri, String value) {
print();
Cookie cookie = new Cookie(uri, value);
for (Cookie existingCookie : cache) {
if ((cookie.getURI().equals(existingCookie.getURI()))
&& (cookie.getName().equals(existingCookie
.getName()))) {
cache.remove(existingCookie);
break;
}
}
System.out.println("Adding to cache: " + cookie);
cache.add(cookie);
}
/**
* Gets all the applicable cookies from a cookie cache for the specified uri
* in the request header.
*
* @param uri
* URI to send cookies to in a request
* @param requestHeaders
* Map from request header field names to lists of field values
* representing the current request headers
* @return Immutable map, with field name "Cookie" to a list of cookies
*/
public Map<String, List<String>> get(URI uri,
Map<String, List<String>> requestHeaders) throws IOException {
// Retrieve all the cookies for matching URI
// Put in comma-separated list
StringBuilder cookies = new StringBuilder();
for (Cookie cookie : cache) {
// Remove cookies that have expired
if (cookie.hasExpired()) {
cache.remove(cookie);
} else if (cookie.matches(uri)) {
if (cookies.length() > 0) {
cookies.append("; ");
}
cookies.append(cookie.toString());
}
}
// Map to return
Map<String, List<String>> cookieMap = new HashMap<String, List<String>>(
requestHeaders);
// Convert StringBuilder to List, store in map
if (cookies.length() > 0) {
List<String> list = Collections.singletonList(cookies.toString());
cookieMap.put("Cookie", list);
}
System.out.println("Cookies: " + cookieMap);
return Collections.unmodifiableMap(cookieMap);
}
}
相关阅读
如何使网站标题快速获取排名?网站SEO标题优化很重要代表一个页面的核心内容。也是搜索引擎抓取首先抓取的内容,每一个网站的SEO
// 获取窗口宽度 if (window.innerWidth) winWidth = window.innerWidth; else if ((document.body) && (document.body.clientWi
APP推广经验:如何做到0推广费获取10万用户,日增1000用户
初做APP,经常从seo实验室吸取运营经验,小有成绩,将我的运营经验拿出来跟大家分享一下!我们的APP在零推广费用的基础上短短几个月获得
主要通过反射获取请求参数,输出JSON格式数据方便以后直接转换对象 string postStr =""; HttpActionContext actionContext = thi
京东白条优惠券兑换码怎么使用以及注意事项有哪些?京东优惠券形式多种多样,那么京东白条优惠券兑换码指的是什么又是怎么使用的呢?