Java調用Http/Https接口(5)--HttpAsyncClient調用Http/Https接口


HttpAsyncClient是HttpClient的異步版本,提供異步調用的api。文中所使用到的軟件版本:Java 1.8.0_191、HttpClient 4.1.4。

1、服務端

參見Java調用Http接口(1)--編寫服務端 

2、調用Http接口

2.1、GET請求

    public static void get() {
        String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白";
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            HttpGet get = new HttpGet(requestPath);
            Future<HttpResponse> future = httpClient.execute(get, null);
            HttpResponse response = future.get();
            System.out.println("GET返回狀態:" + response.getStatusLine());
            HttpEntity responseEntity = response.getEntity();
            System.out.println("GET返回結果:" + EntityUtils.toString(responseEntity));
            
            //回調方式調用
            final CountDownLatch latch = new CountDownLatch(1);
            final HttpGet get2 = new HttpGet(requestPath);
            httpClient.execute(get2, new FutureCallback<HttpResponse>() {
                public void completed(final HttpResponse response) {
                    latch.countDown();
                    System.out.println("GET(回調方式)返回狀態:" + response.getStatusLine());
                    try {
                        System.out.println("GET(回調方式)返回結果:" + EntityUtils.toString(response.getEntity()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                public void failed(final Exception e) {
                    latch.countDown();
                    e.printStackTrace();
                }
                public void cancelled() {
                    latch.countDown();
                    System.out.println("cancelled");
                }

            });
            latch.await();
            
            //流方式調用
            final CountDownLatch latch2 = new CountDownLatch(1);
            final HttpGet get3 = new HttpGet(requestPath);
            HttpAsyncRequestProducer producer3 = HttpAsyncMethods.create(get3);
            AsyncCharConsumer<HttpResponse> consumer3 = new AsyncCharConsumer<HttpResponse>() {
                HttpResponse response;
                @Override
                protected void onResponseReceived(final HttpResponse response) {
                    this.response = response;
                }
                @Override
                protected void releaseResources() {
                }
                @Override
                protected HttpResponse buildResult(final HttpContext context) {
                    return this.response;
                }
                @Override
                protected void onCharReceived(CharBuffer buf, IOControl ioctrl) throws IOException {
                    System.out.println("GET(流方式)返回結果:" + buf.toString());
                }
            };
            httpClient.execute(producer3, consumer3, new FutureCallback<HttpResponse>() {
                public void completed(final HttpResponse response) {
                    latch2.countDown();
                    System.out.println("GET(流方式)返回狀態:" + response.getStatusLine());
                }
                public void failed(final Exception e) {
                    latch2.countDown();
                    e.printStackTrace();
                }
                public void cancelled() {
                    latch2.countDown();
                    System.out.println("cancelled");
                }
            });
            latch2.await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }

2.2、POST請求(發送鍵值對數據)

    public static void post() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/getUser";
            HttpPost post = new HttpPost(requestPath);
            
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("userId", "1000"));
            list.add(new BasicNameValuePair("userName", "李白"));
            post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
            
            Future<HttpResponse> future = httpClient.execute(post, null);
            HttpResponse response = future.get();
            System.out.println("POST返回狀態:" + response.getStatusLine());
            HttpEntity responseEntity = response.getEntity();
            System.out.println("POST返回結果:" + EntityUtils.toString(responseEntity));
            
            //回調方式和流方式調用類似
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }

2.3、POST請求(發送JSON數據)

    public static void post2() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/addUser";
            HttpPost post = new HttpPost(requestPath);
            post.setHeader("Content-type", "application/json");
            String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}";
            post.setEntity(new StringEntity(param, "utf-8"));
            
            Future<HttpResponse> future = httpClient.execute(post, null);
            HttpResponse response = future.get();
            System.out.println("POST json返回狀態:" + response.getStatusLine());
            HttpEntity responseEntity = response.getEntity();
            System.out.println("POST josn返回結果:" + EntityUtils.toString(responseEntity));
            
            //回調方式和流方式調用類似
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }

2.4、上傳文件

    public static void upload() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/upload";
            ZeroCopyPost producer = new ZeroCopyPost(requestPath, new File("d:/a.jpg"), ContentType.create("text/plain"));
            AsyncCharConsumer<HttpResponse> consumer = new AsyncCharConsumer<HttpResponse>() {
                HttpResponse response;
                @Override
                protected void onResponseReceived(final HttpResponse response) {
                    this.response = response;
                }
                @Override
                protected void releaseResources() {
                }
                @Override
                protected HttpResponse buildResult(final HttpContext context) {
                    return this.response;
                }
                @Override
                protected void onCharReceived(CharBuffer buf, IOControl ioctrl) throws IOException {
                    System.out.println("upload返回結果:" + buf.toString());
                }
            };
            Future<HttpResponse> future = httpClient.execute(producer, consumer, null);
            HttpResponse response = future.get();
            System.out.println("upload返回狀態:" + response.getStatusLine());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }

2.5、下載文件

    public static void download() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/download";
            HttpGet get = new HttpGet(requestPath);
            HttpAsyncRequestProducer producer = HttpAsyncMethods.create(get);
            File download = new File("d:/temp/download_" + System.currentTimeMillis() + ".jpg");
            ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) {
                @Override
                protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception {
                    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                        throw new ClientProtocolException("Upload failed: " + response.getStatusLine());
                    }
                    return file;
                }
            };
            Future<File> future = httpClient.execute(producer, consumer, null);
            System.out.println("download文件大小:" + future.get().length());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }

2.6、完整例子

package com.inspur.demo.http.client;

import java.io.File;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.client.methods.AsyncCharConsumer;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.nio.client.methods.ZeroCopyConsumer;
import org.apache.http.nio.client.methods.ZeroCopyPost;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
 * 通過HttpClient調用Http接口
 */
public class HttpAsyncClientCase {
    /**
     *  GET請求
     */
    public static void get() {
        String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白";
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            HttpGet get = new HttpGet(requestPath);
            Future<HttpResponse> future = httpClient.execute(get, null);
            HttpResponse response = future.get();
            System.out.println("GET返回狀態:" + response.getStatusLine());
            HttpEntity responseEntity = response.getEntity();
            System.out.println("GET返回結果:" + EntityUtils.toString(responseEntity));
            
            //回調方式調用
            final CountDownLatch latch = new CountDownLatch(1);
            final HttpGet get2 = new HttpGet(requestPath);
            httpClient.execute(get2, new FutureCallback<HttpResponse>() {
                public void completed(final HttpResponse response) {
                    latch.countDown();
                    System.out.println("GET(回調方式)返回狀態:" + response.getStatusLine());
                    try {
                        System.out.println("GET(回調方式)返回結果:" + EntityUtils.toString(response.getEntity()));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                public void failed(final Exception e) {
                    latch.countDown();
                    e.printStackTrace();
                }
                public void cancelled() {
                    latch.countDown();
                    System.out.println("cancelled");
                }

            });
            latch.await();
            
            //流方式調用
            final CountDownLatch latch2 = new CountDownLatch(1);
            final HttpGet get3 = new HttpGet(requestPath);
            HttpAsyncRequestProducer producer3 = HttpAsyncMethods.create(get3);
            AsyncCharConsumer<HttpResponse> consumer3 = new AsyncCharConsumer<HttpResponse>() {
                HttpResponse response;
                @Override
                protected void onResponseReceived(final HttpResponse response) {
                    this.response = response;
                }
                @Override
                protected void releaseResources() {
                }
                @Override
                protected HttpResponse buildResult(final HttpContext context) {
                    return this.response;
                }
                @Override
                protected void onCharReceived(CharBuffer buf, IOControl ioctrl) throws IOException {
                    System.out.println("GET(流方式)返回結果:" + buf.toString());
                }
            };
            httpClient.execute(producer3, consumer3, new FutureCallback<HttpResponse>() {
                public void completed(final HttpResponse response) {
                    latch2.countDown();
                    System.out.println("GET(流方式)返回狀態:" + response.getStatusLine());
                }
                public void failed(final Exception e) {
                    latch2.countDown();
                    e.printStackTrace();
                }
                public void cancelled() {
                    latch2.countDown();
                    System.out.println("cancelled");
                }
            });
            latch2.await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }
    
    /**
     *  POST請求(發送鍵值對數據)
     */
    public static void post() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/getUser";
            HttpPost post = new HttpPost(requestPath);
            
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("userId", "1000"));
            list.add(new BasicNameValuePair("userName", "李白"));
            post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
            
            Future<HttpResponse> future = httpClient.execute(post, null);
            HttpResponse response = future.get();
            System.out.println("POST返回狀態:" + response.getStatusLine());
            HttpEntity responseEntity = response.getEntity();
            System.out.println("POST返回結果:" + EntityUtils.toString(responseEntity));
            
            //回調方式和流方式調用類似
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }
    
    /**
     *  POST請求(發送json數據)
     */
    public static void post2() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/addUser";
            HttpPost post = new HttpPost(requestPath);
            post.setHeader("Content-type", "application/json");
            String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}";
            post.setEntity(new StringEntity(param, "utf-8"));
            
            Future<HttpResponse> future = httpClient.execute(post, null);
            HttpResponse response = future.get();
            System.out.println("POST json返回狀態:" + response.getStatusLine());
            HttpEntity responseEntity = response.getEntity();
            System.out.println("POST josn返回結果:" + EntityUtils.toString(responseEntity));
            
            //回調方式和流方式調用類似
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }
    
    /**
     * 上傳文件
     */
    public static void upload() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/upload";
            ZeroCopyPost producer = new ZeroCopyPost(requestPath, new File("d:/a.jpg"), ContentType.create("text/plain"));
            AsyncCharConsumer<HttpResponse> consumer = new AsyncCharConsumer<HttpResponse>() {
                HttpResponse response;
                @Override
                protected void onResponseReceived(final HttpResponse response) {
                    this.response = response;
                }
                @Override
                protected void releaseResources() {
                }
                @Override
                protected HttpResponse buildResult(final HttpContext context) {
                    return this.response;
                }
                @Override
                protected void onCharReceived(CharBuffer buf, IOControl ioctrl) throws IOException {
                    System.out.println("upload返回結果:" + buf.toString());
                }
            };
            Future<HttpResponse> future = httpClient.execute(producer, consumer, null);
            HttpResponse response = future.get();
            System.out.println("upload返回狀態:" + response.getStatusLine());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }
    
    /**
     * 下載文件
     */
    public static void download() {
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try {
            httpClient.start();
            String requestPath = "http://localhost:8080/demo/httptest/download";
            HttpGet get = new HttpGet(requestPath);
            HttpAsyncRequestProducer producer = HttpAsyncMethods.create(get);
            File download = new File("d:/temp/download_" + System.currentTimeMillis() + ".jpg");
            ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) {
                @Override
                protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception {
                    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                        throw new ClientProtocolException("Upload failed: " + response.getStatusLine());
                    }
                    return file;
                }
            };
            Future<File> future = httpClient.execute(producer, consumer, null);
            System.out.println("download文件大小:" + future.get().length());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpClient);
        }
    }
    
    private static void close(CloseableHttpAsyncClient httpClient) {
        try {
            if (httpClient != null) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        get();
        post();
        post2();
        upload();
        download();
    }
}
View Code

3、調用Https接口

 與調用Http接口不一樣的部分主要在設置ssl部分,其ssl的設置與HttpsURLConnection很相似(參見Java調用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection調用Http/Https接口);下面用GET請求來演示ssl的設置,其他調用方式類似。

package com.inspur.demo.http.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.util.concurrent.Future;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

import com.inspur.demo.common.util.FileUtil;

/**
 * 通過HttpAsyncClient調用Https接口
 */
public class HttpAsyncClientHttpsCase {

    public static void main(String[] args) {
        CloseableHttpAsyncClient httpAsyncClient = null;
        CloseableHttpAsyncClient httpAsyncClient2 = null;
        CloseableHttpAsyncClient httpAsyncClient3 = null;
        try {
            /*
             * 請求有權威證書的地址
             */
            String requestPath = "https://www.12306.cn/index/";
            httpAsyncClient = HttpAsyncClients.createDefault();
            httpAsyncClient.start();
            HttpGet get = new HttpGet(requestPath);
            Future<HttpResponse> future = httpAsyncClient.execute(get, null);
            HttpResponse response = future.get();
            System.out.println(response.getStatusLine());
            System.out.println("GET1返回結果:" + EntityUtils.toString(response.getEntity(), "utf-8"));
            
            /*
             * 請求自定義證書的地址
             */
            //獲取信任證書庫
            KeyStore trustStore = getkeyStore("jks", "d:/temp/cacerts", "123456");
            //不需要客戶端證書
            requestPath = "https://10.40.x.x:9010/zsywservice";
            httpAsyncClient2 = HttpAsyncClients.custom().setSSLStrategy(getSSLIOSessionStrategy(trustStore)).build();
            httpAsyncClient2.start();
            get = new HttpGet(requestPath);
            future = httpAsyncClient2.execute(get, null);
            response = future.get();
            System.out.println("GET2:" + EntityUtils.toString(response.getEntity()));
            
            //需要客戶端證書
            requestPath = "https://10.40.x.x:9016/zsywservice";
            KeyStore keyStore = getkeyStore("pkcs12", "d:/client.p12", "123456");
            httpAsyncClient3 = HttpAsyncClients.custom().setSSLStrategy(getSSLIOSessionStrategy(keyStore, "123456", trustStore)).build();
            httpAsyncClient3.start();
            get = new HttpGet(requestPath);
            future = httpAsyncClient3.execute(get, null);
            response = future.get();
            System.out.println("GET3返回結果:" + EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(httpAsyncClient);
            close(httpAsyncClient2);
            close(httpAsyncClient3);
        }
    }
    
    public static SSLIOSessionStrategy getSSLIOSessionStrategy(KeyStore keyStore, String keyPassword, KeyStore trustStore) throws Exception {
        SSLContextBuilder bulider = SSLContexts.custom();
        if (keyStore != null) {
            bulider.loadKeyMaterial(keyStore, keyPassword.toCharArray());
        }
        if (keyStore != null) {
            bulider.loadTrustMaterial(trustStore, null);
        } else {
            bulider.loadTrustMaterial(new TrustSelfSignedStrategy());
        }
        SSLContext sslContext = bulider.build();
        // 驗證URL的主機名和服務器的標識主機名是否匹配
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                // if ("xxx".equals(hostname)) {
                // return true;
                // } else {
                // return false;
                // }
                return true;
            }
        };
        SSLIOSessionStrategy strategy = new SSLIOSessionStrategy(sslContext, new String[] { "TLSv1", "TLSv1.2" }, null,
                hostnameVerifier);

        return strategy;
    }
    
    public static SSLIOSessionStrategy getSSLIOSessionStrategy(KeyStore trustStore) throws Exception {
        return getSSLIOSessionStrategy(null, null, trustStore);
    }
    
    private static KeyStore getkeyStore(String type, String filePath, String password) {
        KeyStore keySotre = null;
        FileInputStream in = null;
        try {
            keySotre = KeyStore.getInstance(type);
            in = new FileInputStream(new File(filePath));
            keySotre.load(in, password.toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            FileUtil.close(in);
        }
        return keySotre;
    }
    
    private static void close(CloseableHttpAsyncClient httpClient) {
        try {
            if (httpClient != null) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM