1. 程式人生 > >使用HttpClient進行Get請求

使用HttpClient進行Get請求

cut java sys method .cn exce sta pack cast

package cn.itcast.spider.httpClient; import java.nio.charset.Charset; 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; /** 使用 httpclient 來進行get請求 */ public class HCGet { public static void main(String[] args) throws Exception { // 1.拿到一個httpclient的對象 CloseableHttpClient httpClient = HttpClients.createDefault(); // 2.設置請求方式和請求信息 HttpGet httpGet = new HttpGet("http://www.itcast.cn"); // 3.執行請求 CloseableHttpResponse response = httpClient.execute(httpGet); // 4.獲取返回值 String html = EntityUtils.toString(response.getEntity(),Charset.forName("utf-8")); // 5.打印 System.out.println(html); } }

使用HttpClient進行Get請求