Apache AsyncClient4.1模拟GET、POST请求和响应处理

HTTP 10/17 阅读 744 views次 人气 0
摘要:

AsyncClient是Apache提供的一个异步的HTTP客户端开发包,是基于HttpCore NIO和HttpClient的组件。AsyncClient的产生是为了提供大量并发,对性能要求非常高的HTTP通信,它还提供了基于事件驱动的API。下面给出了GET和POST方法的执行案例。

代码案例:

package com.what21.http.async4_1;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.util.EntityUtils;

public class What21AsyncClientWithGet {

	public static void main(String[] args) {
		// 创建HTTPClient
		CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
		try {
			// 启动reactor
			httpclient.start();
            HttpGet httpget = new HttpGet("http://www.what21.com/");

            System.out.println("执行请求 " + httpget.getRequestLine());
            Future<HttpResponse> future = httpclient.execute(httpget, null);
            HttpResponse response = future.get();
            System.out.println("返回响应: " + response.getStatusLine());
            System.out.println("响应内容:" + EntityUtils.toString(response.getEntity()));
        } catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
            try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }

	}

}
package com.what21.http.async4_1;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.util.EntityUtils;

public class What21AsyncClientWithPost {

	public static void main(String[] args) {
		// 创建HTTPClient
		CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
		try {
			// 启动reactor
			httpclient.start();
			HttpPost httppost = new HttpPost("http://www.what21.com/");
			System.out.println("执行请求 " + httppost.getRequestLine());
			Future<HttpResponse> future = httpclient.execute(httppost, null);
			HttpResponse response = future.get();
			System.out.println("返回响应: " + response.getStatusLine());
			System.out.println("响应内容:" + EntityUtils.toString(response.getEntity()));
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}

HttpCore4.1.2下载地址:http://www.what21.com/a/pan/java.lib_httpcomponents-asyncclient-4.1.2.html

评论

该文章不支持评论!

分享到: