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端資源的返回。
HttpClient包中,三個設置超時的地方:
/* 從連接池中取連接的超時時間*/ ConnManagerParams.setTimeout(params, 1000); /*連接超時*/ HttpConnectionParams.setConnectionTimeout(params, 2000); /*請求超時*/ HttpConnectionParams.setSoTimeout(params, 4000);
ConnectionPoolTimeout:
定義了從ConnectionManager管理的連接池中取出連接的超時時間。
出錯會拋出ConnectionPoolTimeoutException
ConnectionTimeout:
定義了通過網絡與server建立連接的超時時間,Httpclient包中通過一個異步線程去創建與server的socket連接,這就是該socket連接的超時時間。
當連接HTTPserver或者等待HttpConnectionManager管理的一個有效連接超時出錯會拋出ConnectionTimeoutException
SocketTimeout:
這定義了Socket讀數據的超時時間,即從server獲取響應數據須要等待的時間。
當讀取或者接收Socket超時會拋出SocketTimeoutException
httpClient 請求頭設置:
Accept、Connection、User-Agent
Accept:表示瀏覽器可以接收到的類型
*/*:表示所有類型
Connection:分為兩種串行連接、持久連接(keep-alive)、管道化持久連接
1.串行連接中,每次交互都要打開關閉連接
2.持久連接中,第一次交互會打開連接,交互結束后連接並不關閉,下次交互就省去了建立連接的過程。
3.管道化持久連接,允許在持久連接的基礎上,可選的使用傳輸管道,消除傳輸時延。
User-Agent:用戶
