集册 Java实例教程 HttpClientAPI可用于执行表单

HttpClientAPI可用于执行表单

欢马劈雪     最近更新时间:2020-01-02 10:19:05

435
HttpClient API可用于执行基于表单的登录apache http客户端

/*

 * --------------------------------------------------------------------

 * 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/>.

 *

 *///from 时 代 J a v a 公 众 号 - N o w J a v  a . c o m

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 {
// 来 自 NowJava.com - 时  代  Java

    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
展开阅读全文