本文整理汇总了Java中org.apache.http.client.methods.CloseableHttpResponse.getHeaders方法的典型用法代码示例。如果您正苦于以下问题:Java CloseableHttpResponse.getHeaders方法的具体用法?Java CloseableHttpResponse.getHeaders怎么用?Java CloseableHttpResponse.getHeaders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.CloseableHttpResponse
的用法示例。
在下文中一共展示了CloseableHttpResponse.getHeaders方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: send
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
/**
* Makes a HTTP request to the given URL using the given request body,
* parameters and HTTP headers. The parameters are used as URL parameters,
* but if there's a parameter "resourceId", it's added directly to the end
* of the URL. If there's no request body, the value can be null.
*
* @param url URL where the request is sent
* @param params request parameters
* @param requestBody request body
* @param headers HTTP headers to be added to the request
* @return response as string
*/
@Override
public ClientResponse send(String url, String requestBody, Map<String, ?> params, Map<String, String> headers) {
// Build target URL
url = ClientUtil.buildTargetURL(url, params);
// Create HTTP client
CloseableHttpClient httpClient = HttpClients.createDefault();
// Build request
HttpUriRequest request = this.buildtHttpRequest(url, requestBody, headers);
LOGGER.info("Starting HTTP {} operation.", request.getMethod());
// Add headers
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
LOGGER.debug("Add header : \"{}\" = \"{}\"", entry.getKey(), entry.getValue());
request.setHeader(entry.getKey(), entry.getValue());
}
}
try {
//Send the request; It will immediately return the response in HttpResponse object
CloseableHttpResponse response = httpClient.execute(request);
// Get Content-Type header
Header[] contentTypeHeader = response.getHeaders("Content-Type");
String contentType = null;
// Check for null and empty
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
contentType = contentTypeHeader[0].getValue();
}
// Get Status Code
int statusCode = response.getStatusLine().getStatusCode();
// Get reason phrase
String reasonPhrase = response.getStatusLine().getReasonPhrase();
// Get response payload
String responseStr = ClientUtil.getResponseString(response.getEntity());
response.close();
httpClient.close();
LOGGER.debug("REST response content type: \"{}\".", contentType);
LOGGER.debug("REST response status code: \"{}\".", statusCode);
LOGGER.debug("REST response reason phrase: \"{}\".", reasonPhrase);
LOGGER.debug("REST response : \"{}\".", responseStr);
LOGGER.info("HTTP {} operation completed.", request.getMethod());
return new ClientResponse(responseStr, contentType, statusCode, reasonPhrase);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
LOGGER.warn("HTTP {} operation failed. An empty string is returned.", request.getMethod());
return null;
}
}
示例2: getFileName
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
private String getFileName(CloseableHttpResponse response) throws WxErrorException {
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
if(m.matches()){
return m.group(1);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}
示例3: printHTML
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
public static void printHTML(CloseableHttpResponse response) throws IOException {
Header header = response.getHeaders("Location")[0];
CloseableHttpResponse eaa = get(header.getValue(), null);
// printResponse(eaa);
}