當前位置: 首頁>>代碼示例>>Java>>正文


Java CloseableHttpResponse.getHeaders方法代碼示例

本文整理匯總了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;
    }
}
 
開發者ID:vrk-kpa,項目名稱:xrd4j,代碼行數:66,代碼來源:AbstractClient.java

示例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());
}
 
開發者ID:11590692,項目名稱:Wechat-Group,代碼行數:14,代碼來源:MediaDownloadRequestExecutor.java

示例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);
    }
 
開發者ID:Zephery,項目名稱:newblog,代碼行數:6,代碼來源:LibraryUtil.java


注:本文中的org.apache.http.client.methods.CloseableHttpResponse.getHeaders方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。