?? getcookies.html
字號(hào):
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Getting the Cookies from an HTTP Connection
(Java Developers Almanac Example)
</TITLE>
<META CONTENT="Patrick Chan" NAME="AUTHOR">
<META CONTENT="Code Examples from The Java Developers Almanac 1.4" NAME="DESCRIPTION">
<META CONTENT="Addison-Wesley/Patrick Chan" NAME="OWNER">
<META CONTENT="3/20/02" NAME="revision">
<STYLE TYPE="text/css">
<!-- BODY CODE {font-family: Courier, Monospace; font-size: 11pt} TABLE, BODY {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt} PRE {font-family: Courier, Monospace; font-size: 10pt} H3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt} A.eglink {text-decoration: none} A:hover.eglink {text-decoration: underline} -->
</STYLE>
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top"><font face="Times" size="6"><b>The Java Developers Almanac 1.4</b></font>
<br>
Order this book from <a href="/cgi-bin/scripts/redirect.pl?l=ex&url=http://www.amazon.com/exec/obidos/ASIN/0201752808/xeo">Amazon</a>.
</TD>
</TR>
<TR>
<TD align="right" valign="bottom">
<FORM method="get" action="/cgi-bin/search/find.pl">
<INPUT size="25" name="words" type="text"><INPUT value="Search" type="submit">
</FORM>
</TD>
</TR>
</TABLE>
<HR color="#6666cc">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
>
<A HREF="../index.html">List of Packages</A>
>
<B><A HREF="../java.net/pkg.html">java.net</A></B><font color="#666666" SIZE="-2">
[27 examples]
</font>
>
<B><A HREF="../java.net/pkg.html#HTTP">HTTP</A></B><font color="#666666" SIZE="-2">
[4 examples]
</font>
</DIV><P>
<h3>
e141.
Getting the Cookies from an HTTP Connection</h3>
When the server wants to set a cookie in the client, it includes a
response header of the form
<pre>
Set-Cookie: <font color="#0066ff"><i>cookie-value</i></font>; expires=<font color="#0066ff"><i>date</i></font>; path=<font color="#0066ff"><i>path</i></font>; domain=<font color="#0066ff"><i>domain-name</i></font>; secure
</pre>
<code>cookie-value</code> is some arbitrary string data that should be
returned to the server in future URL requests. The life time of the
cookie is specified by <code>expires</code>. If <code>expires</code> is not specified,
the cookie expires at the end of the session. When a URL request is
made, the cookie should be sent along only if <code>domain-name</code> matches
the end of the fully-qualified host name of the URL request and
<code>path</code> matches the beginning of the path of the URL request. If
<code>secure</code> is specified, the cookie should be sent to the server only
through HTTPS.
<pre>
try {
// Create a URLConnection object for a URL
URL url = new URL(<font color="#0066ff"><i>"http://hostname:80"</i></font>);
URLConnection conn = url.openConnection();
// Get all cookies from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
// No more headers
break;
}
if ("Set-Cookie".equalsIgnoreCase(headerName)) {
// Parse cookie
String[] fields = headerValue.split(";\\s*");
String cookieValue = fields[0];
String expires = null;
String path = null;
String domain = null;
boolean secure = false;
// Parse each field
for (int j=1; j<fields.length; j++) {
if ("secure".equalsIgnoreCase(fields[j])) {
secure = true;
} else if (fields[j].indexOf('=') > 0) {
String[] f = fields[j].split("=");
if ("expires".equalsIgnoreCase(f[0])) {
expires = f[1];
} else if ("domain".equalsIgnoreCase(f[0])) {
domain = f[1];
} else if ("path".equalsIgnoreCase(f[0])) {
path = f[1];
}
}
}
// Save the cookie...
}
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
</pre>
Here's a sample of cookies from two websites:
<pre>
B=a43ka6gu6f4n4&b=2; expires=Thu, 15 Apr 2010 20:00:00 GMT;
path=/; domain=.yahoo.com
PREF=ID=e51:TM=686:LM=86:S=BL-w0; domain=.google.com; path=/;
expires=Sun, 17-Jan-2038 19:14:07 GMT
</pre>
<P><table width="600" CELLSPACING="0" CELLPADDING="2" BORDER="0">
<tr>
<td bgcolor="#6666cc" align="center"><font color="#ffffff">
Related Examples
</font></td>
</tr>
</table>
e140. <a class="eglink" href="GetHeaders.html?l=rel">
Getting the Response Headers from an HTTP Connection
</a>
<br>
e142. <a class="eglink" href="SendCookie.html?l=rel">
Sending a Cookie to an HTTP Server
</a>
<br>
e143. <a class="eglink" href="NoRedirect.html?l=rel">
Preventing Automatic Redirects in a HTTP Connection
</a>
<br>
<table width="600" CELLSPACING="0" CELLPADDING="2" BORDER="0">
<tr>
<td align="left">
<br>
See also:
<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Datagram">
Datagram
</a>
<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Encodings">
Encodings
</a>
<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Hostnames%20and%20IP%20Addresses">
Hostnames and IP Addresses
</a>
<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Multicast">
Multicast
</a>
<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Sockets">
Sockets
</a>
<a class="eglink" href="/egs/java.net/pkg.html?l=rel#URLs">
URLs
</a>
</td>
</tr>
</table>
<br>
<br>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="0">
© 2002 Addison-Wesley.
</FONT>
</BODY>
</HTML>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -