java代码中用httpClient调用API接口,简单示例,为了以后复制粘贴
事先准备了几个PHP接口,会已json形式返回它接收到的数据.
用的jar包版本4.5.10
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
- httpClient 发送get请求
public void httpClientGet() {
try {
//用这个对象执行请求
CloseableHttpClient client = HttpClients.createDefault();
//请求地址
URIBuilder uriBuilder = new URIBuilder("https://zy1993.top/get.php");
//准备参数
ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
arrayList.add(new BasicNameValuePair("name", "zhsangSan"));
arrayList.add(new BasicNameValuePair("age", "11"));
//设置参数
uriBuilder.setParameters(arrayList);
//执行
CloseableHttpResponse response = client.execute(
new HttpGet(uriBuilder.build()));
//输出结果
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
结果:
图片.png
- httpClient 发送post请求
public void httpClientPost() {
try {
//用这个对象执行请求
CloseableHttpClient client = HttpClients.createDefault();
//请求地址
URIBuilder uriBuilder = new URIBuilder("https://zy1993.top/post.php");
//准备参数
ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
arrayList.add(new BasicNameValuePair("name", "lisi"));
arrayList.add(new BasicNameValuePair("age", "22"));
//参数封装成实体
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(arrayList,"utf-8");
//设置参数
HttpPost httpPost = new HttpPost(uriBuilder.build());
httpPost.setEntity(entity);
//执行
CloseableHttpResponse response = client.execute(httpPost);
//输出结果
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
执行结果
图片.png
- httpClient 发送post请求(json数据)
public void httpClientPostJson() {
try {
CloseableHttpClient client = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder("https://zy1993.top/json.php");
HttpPost httpPost = new HttpPost(uriBuilder.build());
//准备json
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "wangWu");
jsonObject.put("age", "33");
String jsonString = JSON.toJSONString(jsonObject);
//准备string类型的实体
StringEntity stringEntity = new StringEntity(jsonString, "utf-8");
//设置实体
httpPost.setEntity(stringEntity);
//执行
CloseableHttpResponse response = client.execute(httpPost);
//输出结果
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
结果:
图片.png
网友评论