JAVA基础——HTTP请求
2018-01-10 15:30
411 查看
同步请求
同步 get
public void test1() throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); // Execute 500 request in async for (int i = 0; i < 50; i++) { HttpGet request = new HttpGet("http://xxxx"); request.setHeader("Connection", "close"); CloseableHttpResponse response = httpclient.execute(request); // System.out.println(response.getStatusLine()); response.getStatusLine(); response.close(); } httpclient.close(); }
同步 post
异步请求
Maven引用<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpasyncclient</artifactId> <version>4.1.1</version> </dependency>
异步 get
基本用法:CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();// 默认的配置 try { httpclient.start(); HttpGet request = new HttpGet("http://www.apache.org/"); Future<HttpResponse> future = httpclient.execute(request, null); HttpResponse response = future.get();// 获取结果 System.out.println("Response: " + response.getStatusLine()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done");
httpclient.execute的第二个参数是一个FutureCallback<HttpResponse>的类型的回调,
完整的写法
import java.io.IOException; import java.util.concurrent.CountDownLatch; import shaded.org.apache.http.HttpResponse; import shaded.org.apache.http.client.methods.HttpGet; import shaded.org.apache.http.concurrent.FutureCallback; import shaded.org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import shaded.org.apache.http.impl.nio.client.HttpAsyncClients; import shaded.org.apache.http.util.EntityUtils; public class HttpUtils { public static void AsyncGet(String url,String param){ CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); httpclient.start(); final CountDownLatch latch = new CountDownLatch(1); final HttpGet request = new HttpGet(url+"?"+param); System.out.println(" caller thread id is : " + Thread.currentThread().getId()); httpclient.execute(request, new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { latch.countDown(); System.out.println(" callback thread id is : " + Thread.currentThread().getId()); System.out.println(request.getRequestLine() + "->" + response.getStatusLine()); try { String content = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println(" response content is : " + content); } catch (IOException e) { e.printStackTrace(); } } public void failed(final Exception ex) { latch.countDown(); System.out.println(request.getRequestLine() + "->" + ex); System.out.println(" callback thread id is : " + Thread.currentThread().getId()); } public void cancelled() { latch.countDown(); System.out.println(request.getRequestLine() + " cancelled"); System.out.println(" callback thread id is : " + Thread.currentThread().getId()); } }); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } try { httpclient.close(); } catch (IOException ignore) { } } }
异步 post
相关文章推荐
- Java基础代码库:Java发起Http请求
- java基础---http请求方式详解
- Java基础 Day11 Exception
- 黑马程序员_java基础下
- Java 基础语法
- java实现单链表的基础操作
- java网络编程基础
- Java基础知识资料整理
- JAVA基础知识之多线程——控制线程
- JAVA基础-反射
- 黑马程序员——JAVA基础---操作数组
- Java基础上--(2)
- Java基础(五)----内部类总结
- Java基础-Java编译详解
- Java 基础 第七天
- Java基础——变量
- Java进阶03 IO基础
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)
- Java后端程序员面经(1-2. Java基础之异常篇)
- 黑马程序员--Java基础--07String