package com.open1111.httpclient;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class TestHttp01 {
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault(); // 創(chuàng)建httpclient實(shí)例
HttpGet httpget = new HttpGet( "http://www./" ); // 創(chuàng)建httpget實(shí)例
CloseableHttpResponse response = httpclient.execute(httpget); // 執(zhí)行g(shù)et請(qǐng)求
HttpEntity entity=response.getEntity(); // 獲取返回實(shí)體
System.out.println( "網(wǎng)頁(yè)內(nèi)容:" +EntityUtils.toString(entity, "utf-8" )); // 指定編碼打印網(wǎng)頁(yè)內(nèi)容
response.close(); // 關(guān)閉流和釋放系統(tǒng)資源
}
}
|