in src/main/java/com/alibaba/cloudapi/sdk/client/ApacheHttpClient.java [259:313]
private ApiResponse parseToApiResponse(HttpResponse httpResponse) throws IOException {
ApiResponse result = new ApiResponse(httpResponse.getStatusLine().getStatusCode());
// headers
result.setHeaders(new HashMap<String, List<String>>());
for (Header header : httpResponse.getAllHeaders()) {
List<String> values = result.getHeaders().get(header.getName());
if(values == null){
values = new ArrayList<String>();
}
values.add(header.getValue());
//logger.info("header.getName().toLowerCase() : " + header.getName().toLowerCase());
//logger.info("header.getValue() : " + header.getValue());
result.getHeaders().put(header.getName().toLowerCase() , values);
}
// message
result.setMessage(httpResponse.getStatusLine().getReasonPhrase());
if(httpResponse.getEntity() != null){
// content type
Header contentType = httpResponse.getEntity().getContentType();
if(contentType != null){
result.setContentType(contentType.getValue());
}
else
{
result.setContentType(HttpConstant.CLOUDAPI_CONTENT_TYPE_TEXT);
}
// body
result.setBody(EntityUtils.toByteArray(httpResponse.getEntity()));
String contentMD5 = result.getFirstHeaderValue(HttpConstant.CLOUDAPI_HTTP_HEADER_CA_CONTENT_MD5);
if(null != contentMD5 && !"".equals(contentMD5)){
String localContentMd5 = SignUtil.base64AndMD5(result.getBody());
if(!contentMD5.equalsIgnoreCase(localContentMd5)){
throw new SdkException("Server Content MD5 does not match body content , server md5 is " + contentMD5 + " local md5 is " + localContentMd5 + " body is " + new String(result.getBody()));
}
}
}else{
String contentTypeStr = result.getFirstHeaderValue(HttpConstant.CLOUDAPI_HTTP_HEADER_CONTENT_TYPE);
if(null == contentTypeStr){
contentTypeStr = HttpConstant.CLOUDAPI_CONTENT_TYPE_TEXT;
}
result.setContentType(contentTypeStr);
}
return result;
}