HttpClient是一个高效的、功能丰富的HTTP客户端编程工具包,以编程的方式通过API传输和接收HTTP消息。返回的HttpResponse请求结果通过ResponseHandler<T>处理,可以定制封装功能逻辑。
ResponseHandler<T>是httpclient包内提供的接口,实现函数handleResponse()处理HTTP返回结果:
package org.apache.http.client;
import java.io.IOException;
import org.apache.http.HttpResponse;
public interface ResponseHandler<T> {
T handleResponse(HttpResponse var1) throws ClientProtocolException, IOException;
}
分享封装的4个HTTP返回结果请求类,简化处理逻辑,提高开发效率。
代码文件 |
HTTP返回结果类型 |
RespStr.java |
返回字符串 |
RespJsonObj.java |
返回JSONObject |
RespJsonArr.java |
返回JSONArray |
RespFile.java |
返回二进制文件 |
l 代码
Github下载:https://github.com/jextop/StarterApi/
l RespStr.java:读取HttpResponse返回的内容,格式化为String字符串
- 调用httpResponse.getEntiry()获取返回结果
- 调用ContentType.get()获取内容类型
- 调用ContentType.getCharset()获取编码格式,也可直接使用UTF-8或者不指定编码格式。
- 调用EntityUtils.toString()将返回结果格式化为字符串
public class RespStr implements ResponseHandler<String> {
@Override
public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
HttpEntity entity = httpResponse.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
return EntityUtils.toString(entity, charset);
}
}
l RespJsonObj.java:在返回结果为JSON对象时,转换成JSONObject返回
public class RespJsonObj implements ResponseHandler<JSONObject> {
@Override
public JSONObject handleResponse(HttpResponse resp) throws ClientProtocolException, IOException {
HttpEntity entity = resp.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
String jsonStr = EntityUtils.toString(entity, charset);
// parse JSON object
return JsonUtil.parseObj(jsonStr);
}
}
l RespJsonArr.java:将HTTP请求返回结果转换成JSONArray返回
public class RespJsonArr implements ResponseHandler<JSONArray> {
@Override
public JSONArray handleResponse(HttpResponse resp) throws ClientProtocolException, IOException {
HttpEntity entity = resp.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
String jsonStr = EntityUtils.toString(entity, charset);
// parse JSON array
return JsonUtil.parseArr(jsonStr);
}
}
l RespFile.java:在HTTP返回二进制文件时,从Entity中读取二进制内容,并可从Header中获取文件名称。
public class RespFile implements ResponseHandler<byte[]> {
private static final String fileNameFlag = "attachment;fileName=";
private byte[] bytes;
private String fileName;
public byte[] getBytes() {
return bytes;
}
public String getFileName() {
return fileName;
}
@Override
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
// Header: Content-Disposition: attachment;fileName=abc.txt
Header header = response.getFirstHeader("Content-Disposition");
String headerValue = header.getValue();
if (headerValue.startsWith(fileNameFlag)) {
fileName = headerValue.substring(fileNameFlag.length(), headerValue.length());
}
HttpEntity entity = response.getEntity();
bytes = EntityUtils.toByteArray(entity);
return bytes;
}
}
l HttpService中使用RespStr处理HttpResponse示例:
@Service
public class HttpService {
@Autowired
private HttpClient httpClient;
public <T> T sendRequest(HttpRequestBase httpRequest, ResponseHandler<T> handler) {
try {
return httpClient.execute(httpRequest, handler);
} catch (ClientProtocolException e) {
LogUtil.error("Error when sendRequest", e.getMessage());
} catch (IOException e) {
LogUtil.error("Error when sendRequest", e.getMessage());
}
return null;
}
public <T> T sendHttpGet(String url, ResponseHandler<T> handler) {
return sendRequest(new HttpGet(url), handler);
}
public String sendHttpGet(String url) {
return sendHttpGet(url, new RespStr());
}
}
来源:CSDN
作者:dingxl18
链接:https://blog.csdn.net/xiziyidi/article/details/104117720