Example usage for org.apache.http.client.methods CloseableHttpResponse getAllHeaders

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getAllHeaders

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse getAllHeaders.

Prototype

Header[] getAllHeaders();

Source Link

Usage

From source file:de.zazaz.iot.bosch.indego.App.java

public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
    CloseableHttpClient httpClient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(BASE_URL_PUSHWOOSH + "registerDevice");
    String jsonPost = ""//
            + "{" //
            + "  \"request\":{" //
            + "     \"application\":\"8FF60-0666B\"," //
            + "     \"push_token\":\"124692134091\"," //
            + "     \"hwid\":\"00-0C-29-E8-B1-8D\"," //
            + "     \"timezone\":3600," //
            + "     \"device_type\":3" //
            + "  }" //
            + "}";
    httpPost.setEntity(new StringEntity(jsonPost, ContentType.APPLICATION_JSON));
    CloseableHttpResponse response = httpClient.execute(httpPost);

    System.out.println(response.getStatusLine());
    Header[] headers = response.getAllHeaders();
    for (int i = 0; i < headers.length; i++) {
        System.out.println(headers[i].getName() + ": " + headers[i].getValue());
    }/*from   w  w  w .  jav  a 2  s  .  c om*/
    HttpEntity entity = response.getEntity();
    String contents = EntityUtils.toString(entity);
    System.out.println(contents);

    Thread.sleep(5000);

    HttpPost httpGet = new HttpPost(BASE_URL_PUSHWOOSH + "checkMessage");
    String jsonGet = ""//
            + "{" //
            + "  \"request\":{" //
            + "     \"application\":\"8FF60-0666B\"," //
            + "     \"hwid\":\"00-0C-29-E8-B1-8D\"" //
            + "  }" //
            + "}";
    httpGet.setEntity(new StringEntity(jsonGet, ContentType.APPLICATION_JSON));
    httpClient.execute(httpGet);

    response.close();
}

From source file:com.temenos.useragent.generic.http.DefaultHttpClientHelper.java

/**
 * Builds and returns the {@link HttpHeader http header} from the response
 * message./*from  w ww. j  ava2 s  .  c  o  m*/
 * 
 * @param response
 *            message
 * @param http
 *            header
 */

public static HttpHeader buildResponseHeaders(CloseableHttpResponse httpResponse) {
    HttpHeader header = new HttpHeader();
    for (org.apache.http.Header httpHeader : httpResponse.getAllHeaders()) {
        header.set(httpHeader.getName(), httpHeader.getValue());
    }
    return header;
}

From source file:com.baidubce.util.HttpUtils.java

public static void printResponse(CloseableHttpResponse response) {
    if (!HTTP_VERBOSE) {
        return;/*from www.  j  ava2  s  . c o  m*/
    }
    System.out.println("\n<------------- ");
    StatusLine status = response.getStatusLine();
    System.out.println(status.getStatusCode() + " - " + status.getReasonPhrase());
    Header[] heads = response.getAllHeaders();
    for (Header h : heads) {
        System.out.println(h.getName() + " : " + h.getValue());
    }
}

From source file:com.joyent.manta.http.EncryptedHttpHelperTest.java

/**
 * Builds a fully mocked {@link EncryptionHttpHelper} that is setup to
 * be configured for one cipher/mode and executes requests in another
 * cipher/mode.//from  w  w  w  . j  a  v  a2 s . co m
 */
private static EncryptionHttpHelper fakeEncryptionHttpHelper(String path) throws Exception {
    MantaConnectionContext connectionContext = mock(MantaConnectionContext.class);

    StandardConfigContext config = new StandardConfigContext();

    SupportedCipherDetails cipherDetails = AesCbcCipherDetails.INSTANCE_192_BIT;

    config.setClientEncryptionEnabled(true)
            .setEncryptionPrivateKeyBytes(SecretKeyUtils.generate(cipherDetails).getEncoded())
            .setEncryptionAlgorithm(cipherDetails.getCipherId());

    EncryptionHttpHelper httpHelper = new EncryptionHttpHelper(connectionContext,
            new MantaHttpRequestFactory(UnitTestConstants.UNIT_TEST_URL), config);

    URI uri = URI.create(DEFAULT_MANTA_URL + "/" + path);

    CloseableHttpResponse fakeResponse = mock(CloseableHttpResponse.class);
    StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");

    SupportedCipherDetails objectCipherDetails = AesGcmCipherDetails.INSTANCE_128_BIT;

    Header[] headers = new Header[] {
            // Notice this is a different cipher than the one set in config
            new BasicHeader(MantaHttpHeaders.ENCRYPTION_CIPHER, objectCipherDetails.getCipherId()) };

    when(fakeResponse.getAllHeaders()).thenReturn(headers);
    when(fakeResponse.getStatusLine()).thenReturn(statusLine);

    BasicHttpEntity fakeEntity = new BasicHttpEntity();
    InputStream source = IOUtils.toInputStream("I'm a stream", StandardCharsets.US_ASCII);
    EofSensorInputStream stream = new EofSensorInputStream(source, null);
    fakeEntity.setContent(stream);
    when(fakeResponse.getEntity()).thenReturn(fakeEntity);

    when(connectionContext.getHttpClient()).thenReturn(new FakeCloseableHttpClient(fakeResponse));

    return httpHelper;
}

From source file:com.aliyun.oss.common.comm.DefaultServiceClient.java

protected static ResponseMessage buildResponse(ServiceClient.Request request,
        CloseableHttpResponse httpResponse) throws IOException {

    assert (httpResponse != null);

    ResponseMessage response = new ResponseMessage(request);
    response.setUrl(request.getUri());//w  ww .  j  a  va2s. com
    response.setHttpResponse(httpResponse);

    if (httpResponse.getStatusLine() != null) {
        response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
    }

    if (httpResponse.getEntity() != null) {
        if (response.isSuccessful()) {
            response.setContent(httpResponse.getEntity().getContent());
        } else {
            readAndSetErrorResponse(httpResponse.getEntity().getContent(), response);
        }
    }

    for (Header header : httpResponse.getAllHeaders()) {
        if (HttpHeaders.CONTENT_LENGTH.equals(header.getName())) {
            response.setContentLength(Long.parseLong(header.getValue()));
        }
        response.addHeader(header.getName(), header.getValue());
    }

    HttpUtil.convertHeaderCharsetFromIso88591(response.getHeaders());

    return response;
}

From source file:io.github.theangrydev.thinhttpclient.apache.ApacheHttpClient.java

private Headers adaptHeaders(CloseableHttpResponse apacheResponse) {
    return headers(stream(apacheResponse.getAllHeaders()).map(this::adaptHeader).collect(toList()));
}

From source file:cn.aofeng.demo.httpclient.HttpClientBasic.java

private void processResponse(CloseableHttpResponse response) throws UnsupportedOperationException, IOException {
    try {/*  www .  j  a  v  a  2s  . c  om*/
        // ??
        Header[] headers = response.getAllHeaders();
        for (Header header : headers) {
            _logger.info(header.getName() + ":" + header.getValue());
        }

        // ???
        StatusLine sl = response.getStatusLine();
        _logger.info(String.format("ProtocolVersion:%s, StatusCode:%d, Desc:%s",
                sl.getProtocolVersion().toString(), sl.getStatusCode(), sl.getReasonPhrase()));

        // ??
        HttpEntity entity = response.getEntity();
        _logger.info(String.format("ContentType:%s, Length:%d, Encoding:%s",
                null == entity.getContentType() ? "" : entity.getContentType().getValue(),
                entity.getContentLength(),
                null == entity.getContentEncoding() ? "" : entity.getContentEncoding().getValue()));
        _logger.info(EntityUtils.toString(entity, _charset));
        //            _logger.info( IOUtils.toString(entity.getContent(), _charset) ); // ??????
    } finally {
        response.close();
    }
}

From source file:zz.pseas.ghost.client.GhostClient.java

public CommonsPage postForPage(String url, Map<String, String> params) {
    HttpPost post = new HttpPost(url);
    ArrayList<NameValuePair> arr = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        arr.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }//from   w ww . ja v a  2 s . c o  m

    CommonsPage commonsPage = new CommonsPage();
    try {
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(arr, "utf-8");
        post.setEntity(entity);

        CloseableHttpResponse resp = client.execute(post, context);
        Header[] headers = resp.getAllHeaders();
        for (Header h : headers) {
            String k = h.getName();
            String v = h.getValue();
            commonsPage.addHeader(k, v);
        }

        byte[] bytes = EntityUtils.toByteArray(resp.getEntity());
        commonsPage.setContents(bytes);
    } catch (Exception e) {
        commonsPage.setErrMsg(e.getMessage());
    } finally {
        post.releaseConnection();
    }
    return commonsPage;
}

From source file:RGSCommonUtils.UniversalConnectionInterfaceImp.java

protected String responceToString(CloseableHttpResponse response) throws IOException {
    String result = "";
    Header[] headers = response.getAllHeaders();
    char[] cbuf = new char[10000];
    try {// w  w  w  . ja v  a2  s. co  m
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            InputStreamReader inStream = new InputStreamReader(resEntity.getContent(), "windows-1251");
            BufferedReader br = new BufferedReader(inStream);
            int r = 0;
            while ((r = br.read(cbuf)) != -1) {
                result += String.valueOf(cbuf, 0, r);
            }
        }
    } finally {
        response.close();
    }
    return result;
}

From source file:org.openhim.mediator.engine.connectors.HTTPConnector.java

private String getContentType(CloseableHttpResponse response) {
    if (response.getAllHeaders() == null) {
        return null;
    }//from   w  w  w .  jav  a2 s . c o  m

    for (Header hdr : response.getAllHeaders()) {
        if ("Content-Type".equalsIgnoreCase(hdr.getName())) {
            return hdr.getValue();
        }
    }

    return null;
}