Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As the documents of Apache commons httpclient states in the <a href="http://hc.apache.org/httpclient-3.x/cookies.html" rel="nofollow" title="httpclient Cookie Handling">HttpClient Cookie handling part</a>: <code>HttpClient supports automatic management of cookies, including allowing the server to set cookies and automatically return them to the server when required. It is also possible to manually set cookies to be sent to the server.</code></p> <p>Whenever the http client receives cookies they are persisted into <code>HttpState</code> and added automatically to the new request. This is the default behavior.</p> <p>In the following example code, we can see the cookies returned by two GET requests. We can't see directly the cookies sent to the server, but we can use a tool such as a protocol/net sniffer or <code>ngrep</code> to see the data transmitted over the network:</p> <pre><code>import java.io.IOException; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; public class HttpTest { public static void main(String[] args) throws HttpException, IOException { String url = "http://www.whatarecookies.com/cookietest.asp"; HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); HttpMethod method = new GetMethod(url); int res = client.executeMethod(method); System.out.println("Result: " + res); printCookies(client.getState()); method = new GetMethod(url); res = client.executeMethod(method); System.out.println("Result: " + res); printCookies(client.getState()); } public static void printCookies(HttpState state){ System.out.println("Cookies:"); Cookie[] cookies = state.getCookies(); for (Cookie cookie : cookies){ System.out.println(" " + cookie.getName() + ": " + cookie.getValue()); } } } </code></pre> <p>This is the output:</p> <pre><code>Result: 200 Cookies: active_template::468: %2Fresponsive%2Fthree_column_inner_ad3b74de5a1c2f311bee7bca5c368aaa4e:b326b5062b2f0e69046810717534cb09 Result: 200 Cookies: active_template::468: %2Fresponsive%2Fthree_column_inner_ad%2C+3b74de5a1c2f311bee7bca5c368aaa4e%3Db326b5062b2f0e69046810717534cb09 3b74de5a1c2f311bee7bca5c368aaa4e: b326b5062b2f0e69046810717534cb09 </code></pre> <p>Here is an excerpt of <code>ngrep</code>:</p> <pre><code>MacBook$ sudo ngrep -W byline -d en0 "" host www.whatarecookies.com interface: en0 (192.168.11.0/255.255.255.0) filter: (ip) and ( dst host www.whatarecookies.com ) ##### T 192.168.11.70:56267 -&gt; 54.228.218.117:80 [AP] GET /cookietest.asp HTTP/1.1. User-Agent: Jakarta Commons-HttpClient/3.1. Host: www.whatarecookies.com. . #### T 54.228.218.117:80 -&gt; 192.168.11.70:56267 [A] HTTP/1.1 200 OK. Server: nginx/1.4.0. Date: Wed, 27 Nov 2013 10:22:14 GMT. Content-Type: text/html; charset=iso-8859-1. Content-Length: 36397. Connection: keep-alive. Vary: Accept-Encoding. Vary: Cookie,Host,Accept-Encoding. Set-Cookie: active_template::468=%2Fresponsive%2Fthree_column_inner_ad; expires=Fri, 29-Nov-2013 10:22:01 GMT; path=/; domain=whatarecookies.com; httponly. Set-Cookie: 3b74de5a1c2f311bee7bca5c368aaa4e=b326b5062b2f0e69046810717534cb09; expires=Thu, 27-Nov-2014 10:22:01 GMT. X-Middleton-Response: 200. Cache-Control: max-age=0, no-cache. X-Mod-Pagespeed: 1.7.30.1-3609. . &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd"&gt; ... ## T 192.168.11.70:56267 -&gt; 54.228.218.117:80 [AP] GET /cookietest.asp HTTP/1.1. User-Agent: Jakarta Commons-HttpClient/3.1. Host: www.whatarecookies.com. Cookie: active_template::468=%2Fresponsive%2Fthree_column_inner_ad. Cookie: 3b74de5a1c2f311bee7bca5c368aaa4e=b326b5062b2f0e69046810717534cb09. . ## T 54.228.218.117:80 -&gt; 192.168.11.70:56267 [A] HTTP/1.1 200 OK. Server: nginx/1.4.0. Date: Wed, 27 Nov 2013 10:22:18 GMT. Content-Type: text/html; charset=iso-8859-1. Content-Length: 54474. Connection: keep-alive. Vary: Accept-Encoding. Vary: Cookie,Host,Accept-Encoding. Set-Cookie: active_template::468=%2Fresponsive%2Fthree_column_inner_ad%2C+3b74de5a1c2f311bee7bca5c368aaa4e%3Db326b5062b2f0e69046810717534cb09; expires=Fri, 29-Nov-2013 10:22:05 GMT; path=/; domain=whatarecookies.com; httponly. Set-Cookie: 3b74de5a1c2f311bee7bca5c368aaa4e=b326b5062b2f0e69046810717534cb09; expires=Thu, 27-Nov-2014 10:22:05 GMT. X-Middleton-Response: 200. Cache-Control: max-age=0, no-cache. X-Mod-Pagespeed: 1.7.30.1-3609. . &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd"&gt; ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload