您的位置:首页 > 理论基础 > 计算机网络

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: