使用HttpClient进行Get请求

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方式通信(使用HttpGet获取网页数据)


下一篇:SQL的查询语句