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


Java CookieSpec.match方法代码示例

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


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

示例1: getCookies

import org.apache.commons.httpclient.cookie.CookieSpec; //导入方法依赖的package包/类
/**
 * Returns an array of {@link Cookie cookies} in this HTTP 
 * state that match the given request parameters.
 * 
 * @param domain the request domain
 * @param port the request port
 * @param path the request path
 * @param secure <code>true</code> when using HTTPS
 * 
 * @return an array of {@link Cookie cookies}.
 * 
 * @see #getCookies()
 * 
 * @deprecated use CookieSpec#match(String, int, String, boolean, Cookie)
 */
public synchronized Cookie[] getCookies(
    String domain, 
    int port, 
    String path, 
    boolean secure
) {
    LOG.trace("enter HttpState.getCookies(String, int, String, boolean)");

    CookieSpec matcher = CookiePolicy.getDefaultSpec();
    ArrayList list = new ArrayList(cookies.size());
    for (int i = 0, m = cookies.size(); i < m; i++) {
        Cookie cookie = (Cookie) (cookies.get(i));
        if (matcher.match(domain, port, path, secure, cookie)) {
            list.add(cookie);
        }
    }
    return (Cookie[]) (list.toArray(new Cookie[list.size()]));
}
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:34,代码来源:HttpState.java

示例2: main

import org.apache.commons.httpclient.cookie.CookieSpec; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        // 'developer.java.sun.com' has cookie compliance problems
        // Their session cookie's domain attribute is in violation of the RFC2109
        // We have to resort to using compatibility cookie policy

        GetMethod authget = new GetMethod("/servlet/SessionServlet");

        client.executeMethod(authget);
        System.out.println("Login form get: " + authget.getStatusLine().toString()); 
        // release any connection resources used by the method
        authget.releaseConnection();
        // See if we got any cookies
        CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
        Cookie[] initcookies = cookiespec.match(
            LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
        System.out.println("Initial set of cookies:");    
        if (initcookies.length == 0) {
            System.out.println("None");    
        } else {
            for (int i = 0; i < initcookies.length; i++) {
                System.out.println("- " + initcookies[i].toString());    
            }
        }
        
        PostMethod authpost = new PostMethod("/servlet/SessionServlet");
        // Prepare login parameters
        NameValuePair action   = new NameValuePair("action", "login");
        NameValuePair url      = new NameValuePair("url", "/index.html");
        NameValuePair userid   = new NameValuePair("UserId", "userid");
        NameValuePair password = new NameValuePair("Password", "password");
        authpost.setRequestBody( 
          new NameValuePair[] {action, url, userid, password});
        
        client.executeMethod(authpost);
        System.out.println("Login form post: " + authpost.getStatusLine().toString()); 
        // release any connection resources used by the method
        authpost.releaseConnection();
        // See if we got any cookies
        // The only way of telling whether logon succeeded is 
        // by finding a session cookie
        Cookie[] logoncookies = cookiespec.match(
            LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
        System.out.println("Logon cookies:");    
        if (logoncookies.length == 0) {
            System.out.println("None");    
        } else {
            for (int i = 0; i < logoncookies.length; i++) {
                System.out.println("- " + logoncookies[i].toString());    
            }
        }
        // Usually a successful form-based login results in a redicrect to 
        // another url
        int statuscode = authpost.getStatusCode();
        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
            (statuscode == HttpStatus.SC_SEE_OTHER) ||
            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
            Header header = authpost.getResponseHeader("location");
            if (header != null) {
                String newuri = header.getValue();
                if ((newuri == null) || (newuri.equals(""))) {
                    newuri = "/";
                }
                System.out.println("Redirect target: " + newuri); 
                GetMethod redirect = new GetMethod(newuri);

                client.executeMethod(redirect);
                System.out.println("Redirect: " + redirect.getStatusLine().toString()); 
                // release any connection resources used by the method
                redirect.releaseConnection();
            } else {
                System.out.println("Invalid redirect");
                System.exit(1);
            }
        }
    }
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:81,代码来源:FormLoginDemo.java


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