HttpClient是一個高效的、功能豐富的HTTP客戶端編程工具包,以編程的方式經過API傳輸和接收HTTP消息。返回的HttpResponse請求結果經過ResponseHandler<T>處理,能夠定製封裝功能邏輯。
java
ResponseHandler<T>是httpclient包內提供的接口,實現函數handleResponse()處理HTTP返回結果:git
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;
}github
分享封裝的4個HTTP返回結果請求類,簡化處理邏輯,提升開發效率。apache
代碼文件編程 |
HTTP返回結果類型json |
RespStr.javaide |
返回字符串函數 |
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()獲取編碼格式
- 調用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());
}
}