综述
- 在日常项目开发中,需要调用第三方接口数据,常用的方法有传统JDK自带的URLConnection,Apache Jakarta Common下的子项目HttpClient ,Spring的RestTemplate。
在SpringBoot项目下,属于注册中心的API可直接用RestTemplate发请求,其他API用HttpClient 居多。*
HttpClient
- 概念不赘述,反正也没人看*
get请求
/**
* get请求
*author:fantabulous24
* @date 2020年7月9日 下午3:24:50
*/
public static String htget(String url) {
CloseableHttpClient httpclient = HttpClients.createDefault();
String result = "";
try {
// 创建httpget.
HttpGet httpget = new HttpGet(url);
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
// result =result + entity.toString();
result =result + EntityUtils.toString(entity);
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: "
+ entity.getContentLength());
// 打印响应内容
System.out.println("Response content: "
+ EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
patch请求
/**
* patch请求
*author:fantabulous24
* @date 2020年7月9日 下午3:24:50
*/
public static JSONObject HttpPatch(String url) {
JSONObject resultObj = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPatch httpPatch = new HttpPatch(url);
httpPatch.setHeader("Content-type", "application/json");
httpPatch.setHeader("Charset", HTTP.UTF_8);
httpPatch.setHeader("Accept", "application/json");
httpPatch.setHeader("Accept-Charset", HTTP.UTF_8);
try {
StringEntity entity = new StringEntity("{\n\t\"instances\":0\n}", HTTP.UTF_8);
httpPatch.setEntity(entity);
HttpResponse response = httpClient.execute(httpPatch);
resultObj = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
return resultObj;
}
post请求
/**
* post请求,偷懒没写成函数
*author:fantabulous24
* @date 2020年7月9日 下午3:24:50
*/
// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httpPost远程连接实例
HttpPost httpPost = new HttpPost("http://192.168.171.29:8080/v2/apps");
// 配置请求参数实例
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000)// 设置连接主机服务超时时间
.setConnectionRequestTimeout(10000)// 设置连接请求超时时间
.setSocketTimeout(30000)// 设置读取数据连接超时时间
.build();
// 为httpPost实例设置配置
httpPost.setConfig(requestConfig);
// 设置请求头
httpPost.addHeader("content-type", "application/json;charset=utf-8");
// 封装post请求参数
httpPost.setEntity(new StringEntity(s, Charset.forName("UTF-8")));
// httpClient对象执行post请求,并返回响应参数对象
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
// 从响应对象中获取响应内容
String result = "";
try {
result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("qqqqqqqq"+result);
} catch (IOException e) {
e.printStackTrace();
}
RestTemplate
- get、post获取数据类型前后须一致*
get请求
/**
* get请求
*author:fantabulous24
* @date 2020年7月9日 下午3:24:50
*/
ResponseEntity<Object> tuple = restTemplate.getForEntity("url", Object.class);
post请求
/**
* post请求
*author:fantabulous24
* @date 2020年7月9日 下午3:24:50
*/
ResponseEntity<JSONObject> tuple = restTemplate.postForEntity("url", getProperty,JSONObject.class);
如有不当之处,欢迎指正。您的指点是我进步的捷径。