⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getcookies.htm

📁 这个压缩包里的都是超级经典的java例子
💻 HTM
字号:
<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">
<META CONTENT="no-cache" HTTP-EQUIV="Pragma">
<LINK href="/almanac.css" media="screen" type="text/css" rel="stylesheet">
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD></TD>
</TR>
</TABLE>
<br>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD></TD>
</TR>
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top">
<h1>The Java Developers Almanac 1.4</h1>
<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">
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD valign="top"><script type="text/javascript">
<!--
google_ad_client = "pub-6001183370374757";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel = "4777242811";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "6666CC";
google_color_url = "6666CC";
google_color_text = "000000";
//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD valign="top">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
    &gt;
    <A HREF="../index.html">List of Packages</A>
    &gt;

    
    <A HREF="../java.net/pkg.html">java.net</A><font color="#666666" class="xsmall-font">
        &nbsp;[27 examples]
    </font>
        &gt;
        <B><A HREF="../java.net/pkg.html#HTTP">HTTP</A></B><font color="#666666" class="xsmall-font">
            &nbsp;[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 &amp;&amp; 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&lt;fields.length; j++) {
                    if ("secure".equalsIgnoreCase(fields[j])) {
                        secure = true;
                    } else if (fields[j].indexOf('=') &gt; 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&amp;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">
            &nbsp;<b>Related Examples</b></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>&nbsp;&nbsp;

<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Encodings">
    Encodings
</a>&nbsp;&nbsp;

<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Hostnames%20and%20IP%20Addresses">
    Hostnames and IP Addresses
</a>&nbsp;&nbsp;

<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Multicast">
    Multicast
</a>&nbsp;&nbsp;

<a class="eglink" href="/egs/java.net/pkg.html?l=rel#Sockets">
    Sockets
</a>&nbsp;&nbsp;

<a class="eglink" href="/egs/java.net/pkg.html?l=rel#URLs">
    URLs
</a>&nbsp;&nbsp;

</td>
</tr>
</table>

<br>

<br>
<FONT class="xsmall-font">
&copy; 2002 Addison-Wesley.
</FONT></TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD valign="top"><A href="http://compositesw.com/devzone?ref=javaalmanac"><IMG alt="Click Here" height="600" width="120" border="0" src="/csw_oad_120x600_final.gif"></A></TD>
</TR>
</TABLE>
</BODY>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META CONTENT="NO-CACHE" HTTP-EQUIV="PRAGMA">
</HEAD>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -