Java HttpClient

Java HttpClient

August 15, 2017
Java, Http

来记录一下Java 发起Http请求的方法。

这里使用的是org.apache.http包中的一些封装工具。

  1. 首先实例化一个client:
    CloseableHttpClient httpclient = HttpClients.createDefault();
  1. 然后来实例化一个URIBuild:
    URIBuilder builder = new URIBuilder(url);
  1. 如果Http请求带有参数,就设置在uri中:

    builder.setParameter(key, value);
  1. 实例化一个Httpget/Httppost:
    HttpGet httpget = new HttpGet(builder.build());
    HttpPost httppost = new HttpPost(builder.build());
  1. 你可以设置请求的Header:
	httpget.setHeader("Accept", "application/json");

        // 接收json数据格式
  1. 发送请求:
    CloseableHttpResponse response = httpclient.execute(httpget);
  1. 查看请求结果:
    if (response.getStatusLine().getStatusCode() == 200) {
        String content = EntityUtils.toString(response.getEntity(), "utf-8");
    }

更多设置可以直接补全出来看。