当前位置: 首页>>代码示例>>Java>>正文


Java HttpClient.setState方法代码示例

本文整理汇总了Java中org.apache.commons.httpclient.HttpClient.setState方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClient.setState方法的具体用法?Java HttpClient.setState怎么用?Java HttpClient.setState使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.httpclient.HttpClient的用法示例。


在下文中一共展示了HttpClient.setState方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import org.apache.commons.httpclient.HttpClient; //导入方法依赖的package包/类
/**
 *
 * 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();
}
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:65,代码来源:CookieDemoApp.java


注:本文中的org.apache.commons.httpclient.HttpClient.setState方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。