This tip shows how to use cookies. The application below sets an HTTP cookie and updates the cookie's value across multiple HTTP GET requests.
/*
* $Header:
* $Revision$
* $Date$
* ====================================================================
*
* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
/**
*
* This is a sample application that demonstrates
* how to use the Jakarta HttpClient API.
*
* This application sets an HTTP cookie and
* updates the cookie's value across multiple
* HTTP GET requests.
*
* @author Sean C. Sullivan
* @author Oleg Kalnichevski
*
*/
public class CookieDemoApp {
/**
*
* Usage:
* java CookieDemoApp http://mywebserver:80/
*
* @param args command line arguments
* Argument 0 is a URL to a web server
*
*
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: java CookieDemoApp <url>");
System.err.println("<url> The url of a webpage");
System.exit(1);
}
// Get target URL
String strURL = args[0];
System.out.println("Target URL: " + strURL);
// Get initial state object
HttpState initialState = new HttpState();
// Initial set of cookies can be retrieved from persistent storage
// and re-created, using a persistence mechanism of choice,
Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff",
"/", null, false);
// and then added to your HTTP state instance
initialState.addCookie(mycookie);
// Get HTTP client instance
HttpClient httpclient = new HttpClient();
httpclient.getHttpConnectionManager().
getParams().setConnectionTimeout(30000);
httpclient.setState(initialState);
// RFC 2101 cookie management spec is used per default
// to parse, validate, format & match cookies
httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
// A different cookie management spec can be selected
// when desired
//httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);
// Netscape Cookie Draft spec is provided for completeness
// You would hardly want to use this spec in real life situations
// httppclient.getParams().setCookiePolicy(
// CookiePolicy.BROWSER_COMPATIBILITY);
// Compatibility policy is provided in order to mimic cookie
// management of popular web browsers that is in some areas
// not 100% standards compliant
// Get HTTP GET method
GetMethod httpget = new GetMethod(strURL);
// Execute HTTP GET
int result = httpclient.executeMethod(httpget);
// Display status code
System.out.println("Response status code: " + result);
// Get all the cookies
Cookie[] cookies = httpclient.getState().getCookies();
// Display the cookies
System.out.println("Present cookies: ");
for (int i = 0; i < cookies.length; i++) {
System.out.println(" - " + cookies[i].toExternalForm());
}
// Release current connection to the connection pool
// once you are done
httpget.releaseConnection();
}
}
分享到:
相关推荐
然而,需要注意的是,`commons-httpclient-3.1.jar`已经是较旧的版本,现代项目可能倾向于使用更新的Apache HttpClient库(如HttpClient 4.x或HttpComponents Client)或其它现代HTTP客户端,如OkHttp。
http://jakarta.apache.org/commons/httpclient/ org.apache.commons.httpclient.URI org.apache.commons.httpclient.Wire org.apache.commons.httpclient.Cookie org.apache.commons.httpclient.Header org.apache....
4. **Cookie管理**:在进行会话管理时,HttpClient可以方便地处理服务器返回的cookies。 5. **认证**:HttpClient支持多种认证机制,如Basic认证、Digest认证等,这对于访问受保护的Web服务非常重要。 6. **代理支持...
3. **HTTP协议支持**:HttpClient实现了HTTP/1.0和HTTP/1.1协议,支持Cookie管理、URL重定向、身份验证(如Basic Auth和Digest Auth)以及HTTPS安全通信。 4. **请求和响应处理**:HttpClient提供了丰富的API,使得...
标题中的"commons-httpclient.jar,commons-codec.jar,commons-logging.jar"是Java开发中常用的三个库文件,它们各自服务于不同的功能领域。 首先,`commons-httpclient.jar`是Apache Commons HttpClient库,它是...
在项目中集成HttpClient,通常需要将`commons-httpclient-3.1-rc1.jar`添加到项目的类路径中,然后通过创建HttpClient实例,设置必要的配置,并调用相关方法发起HTTP请求。 接下来是`commons-logging.jar`,它是...
2. 其他依赖库文件:commons-codec-1.3.jar,因为HttpClient依赖Apache Commons Codec库,用于编码解码操作。如果没有此库,运行时会出现NoClassDefFoundError错误。 3. 日志组件:commons-logging.jar,HttpClient ...
2. **commons-httpclient-3.0.1.jar**: 这就是Apache Commons HttpClient的核心库。这个版本3.0.1是该库的一个稳定版本,它包含了处理HTTP请求和响应的类和方法。HttpClient提供了异步和同步两种模式来发送GET、POST...
然而,需要注意的是,随着Java社区的发展,现在Spring框架中的RestTemplate和最新的HttpClient(Java 11及更高版本)已经成为了更推荐的选择,尽管如此,Apache Commons HttpClient仍是一个值得学习和理解的优秀库,...
**Apache Commons HttpClient 3.1** Apache Commons HttpClient是一个成熟的Java库,专门用于执行HTTP请求。它为Java开发者提供了一个高级接口,使得发送HTTP请求和处理响应变得简单。以下是一些HttpClient 3.1的...
Apache Commons HttpClient 3.1 是一个功能强大的Java HTTP客户端库,它提供了全面的HTTP协议支持,包括基本的GET和POST请求、Cookie管理、重定向处理以及HTTPS通信。这个库是Java开发者进行网络编程时的重要工具,...
1. Commons-HTTPClient组件的主库文件:commons-httpclient-3.1.jar 2. 编码解码支持库:commons-codec-1.3.jar,用于处理编码相关的问题。 3. 日志功能组件:可能需要Commons-Logging,以便记录和调试网络通信的...
这个库不仅支持基本的HTTP操作,还支持HTTPS、代理设置、Cookie管理、连接池等高级功能。 首先,HttpClient的配置是其灵活性的关键。开发者可以通过`HttpParams`对象来定制各种参数,如超时时间、重试策略、编码...
《Apache Commons HttpClient 3.1详解》 Apache Commons HttpClient 是一个功能强大的Java库,专为实现客户端HTTP通信而设计。这个3.1版本是HttpClient的一个重要里程碑,它提供了丰富的功能和改进,使得开发者能够...
3. **Cookie管理**:`commons-httpclient`处理了Cookie的发送和接收,自动维护Cookie状态,支持Cookie策略,如只接受来自特定域的Cookie,或者限制Cookie的数量。 4. **认证机制**:库提供了多种认证机制的支持,...
这个库的核心是`commons-httpclient.jar`,而我们所提到的`commons-codec-1.3.jar`和`commons-logging-api-1.1.jar`则是HttpClient依赖的两个关键组件。 首先,`commons-httpclient.jar`是Apache HttpClient的主要...
Apache Commons HttpClient 是一个高度可定制的HTTP客户端实现,支持从简单的GET请求到复杂的POST操作,包括文件上传、Cookie管理、重定向处理、HTTP认证等。它提供了一个强大的API,使得开发者能够轻松地创建、发送...
HttpClient 是Apache Commons项目的一部分,它提供了一种灵活、可扩展的框架,用于执行各种HTTP操作,如GET、POST、PUT等。HttpClient 3.1是其的一个稳定版本,虽然现在已经有了更新的版本(如4.x系列),但在某些...
《Apache Commons HttpClient 3.0-rc2:Web服务接口通信的关键库》 Apache Commons HttpClient 是一个成熟的、广泛使用的Java库,专为处理HTTP客户端通信而设计。在本篇文章中,我们将深入探讨这个库的3.0-rc2版本...
3. **Cookie管理**:通过`CookiePolicy`和`CookieStore`,HttpClient可以处理服务器返回的Cookie,保持会话状态,实现登录认证等功能。 4. **URL编码与解码**:HttpClient内置了对URL编码和解码的支持,防止URL中的...