欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

同步和异步发送 http 请求的 java 实现

最编程 2024-07-16 14:13:31
...

http同步请求 一般使用httpClient实现

 private void sendRequest() throws Exception{
    String path ="/statistic/info";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    // 创建一个 GET 请求
    HttpGet httpGet = new HttpGet(path);
    // 执行请求
    CloseableHttpResponse response =httpClient.execute(httpGet);
    //取响应的结果
    int statusCode =response.getStatusLine().getStatusCode();
    System.out.println(statusCode);
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(content);
    //关闭httpclient
    response.close();
    httpClient.close();
}

http异步请求

1.通过httpAsncClient实现

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

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class Main {
    public static void main(String[] argv) {
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        httpclient.start();

        final CountDownLatch latch = new CountDownLatch(1);
        final HttpGet request = new HttpGet("https://www.alipay.com/");

        System.out.println(" caller thread id is : " + Thread.currentThread().getId());

        httpclient.execute(request, new FutureCallback<HttpResponse>() {
 @Override
            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();
                }
            }
 @Override
            public void failed(final Exception ex) {
                latch.countDown();
                System.out.println(request.getRequestLine() + "->" + ex);
                System.out.println(" callback thread id is : " + Thread.currentThread().getId());
            }
 @Override
            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) {

        }
    }
}

2.还是通过同步请求,但是另起一个线程来计时,这种本质上有一个线程始终在堵塞,等待web端资源的返回。

原文地址:https://www.cnblogs.com/pass-ion/p/14073468.html

推荐阅读