美文网首页
httpClient 如何使用cookie

httpClient 如何使用cookie

作者: yahzon | 来源:发表于2020-01-03 11:18 被阅读0次

    何必百度,官网的Examples不香吗

    /*
     * ====================================================================
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you 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/>.
     *
     */
    package org.apache.http.examples.client;
    
    import java.net.URI;
    import java.util.List;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.apache.http.client.methods.RequestBuilder;
    import org.apache.http.cookie.Cookie;
    import org.apache.http.impl.client.BasicCookieStore;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    /**
     * A example that demonstrates how HttpClient APIs can be used to perform
     * form-based logon.
     */
    public class ClientFormLogin {
    
        public static void main(String[] args) throws Exception {
            BasicCookieStore cookieStore = new BasicCookieStore();
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setDefaultCookieStore(cookieStore)
                    .build();
            try {
                HttpGet httpget = new HttpGet("https://someportal/");
                CloseableHttpResponse response1 = httpclient.execute(httpget);
                try {
                    HttpEntity entity = response1.getEntity();
    
                    System.out.println("Login form get: " + response1.getStatusLine());
                    EntityUtils.consume(entity);
    
                    System.out.println("Initial set of cookies:");
                    List<Cookie> cookies = cookieStore.getCookies();
                    if (cookies.isEmpty()) {
                        System.out.println("None");
                    } else {
                        for (int i = 0; i < cookies.size(); i++) {
                            System.out.println("- " + cookies.get(i).toString());
                        }
                    }
                } finally {
                    response1.close();
                }
    
                HttpUriRequest login = RequestBuilder.post()
                        .setUri(new URI("https://someportal/"))
                        .addParameter("IDToken1", "username")
                        .addParameter("IDToken2", "password")
                        .build();
                CloseableHttpResponse response2 = httpclient.execute(login);
                try {
                    HttpEntity entity = response2.getEntity();
    
                    System.out.println("Login form get: " + response2.getStatusLine());
                    EntityUtils.consume(entity);
    
                    System.out.println("Post logon cookies:");
                    List<Cookie> cookies = cookieStore.getCookies();
                    if (cookies.isEmpty()) {
                        System.out.println("None");
                    } else {
                        for (int i = 0; i < cookies.size(); i++) {
                            System.out.println("- " + cookies.get(i).toString());
                        }
                    }
                } finally {
                    response2.close();
                }
            } finally {
                httpclient.close();
            }
        }
    }
    

    https://hc.apache.org/httpcomponents-client-4.5.x/httpclient/examples/org/apache/http/examples/client/ClientFormLogin.java
    一个很好的httpClient讲解文章:https://blog.csdn.net/justry_deng/article/details/81042379
    非本文内容。

    相关文章

      网友评论

          本文标题:httpClient 如何使用cookie

          本文链接:https://www.haomeiwen.com/subject/rdlzoctx.html