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 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端资源的返回。 HttpClient包中,三个设置超时的地⽅: /* 从连接池中取连接的超时时间*/ ConnManagerParams.setTimeout(params, 1000); /*连接超时*/ HttpConnectionParams.setConnectionTimeout(params, 2000); /*请求超时*/ HttpConnectionParams.setSoTimeout(params, 4000); ConnectionPoolTimeout: 定义了从ConnectionManager管理的连接池中取出连接的超时时间。出错会抛出ConnectionPoolTimeoutException ConnectionTimeout: 定义了通过⽹络与server建⽴连接的超时时间,Httpclient包中通过⼀个异步线程去创建与server的socket连接,这就是该socket连接的超时时间。 当连接HTTPserver或者等待HttpConnectionManager管理的⼀个有效连接超时出错会抛出ConnectionTimeoutExceptionSocketTimeout: 这定义了Socket读数据的超时时间,即从server获取响应数据须要等待的时间。当读取或者接收Socket超时会抛出SocketTimeoutException httpClient 请求头设置: Accept、Connection、User-AgentAccept:表⽰浏览器可以接收到的类型*/*:表⽰所有类型 Connection:分为两种串⾏连接、持久连接(keep-alive)、管道化持久连接1.串⾏连接中,每次交互都要打开关闭连接 2.持久连接中,第⼀次交互会打开连接,交互结束后连接并不关闭,下次交互就省去了建⽴连接的过程。3.管道化持久连接,允许在持久连接的基础上,可选的使⽤传输管道,消除传输时延。User-Agent:⽤户 因篇幅问题不能全部显示,请点此查看更多更全内容