Example usage for org.apache.http.message BasicHeader BasicHeader

List of usage examples for org.apache.http.message BasicHeader BasicHeader

Introduction

In this page you can find the example usage for org.apache.http.message BasicHeader BasicHeader.

Prototype

public BasicHeader(String str, String str2) 

Source Link

Usage

From source file:tv.icntv.common.HttpClientUtil.java

/**
 * Get content by url as string//from w w  w  .  jav a 2s . c o m
 *
 * @param url original url
 * @return page content
 * @throws java.io.IOException
 */
public static String getContent(String url) throws IOException {
    // construct request
    HttpGet request = new HttpGet(url);
    request.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"));

    request.setConfig(RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT).build());
    // construct response handler
    ResponseHandler<String> handler = new ResponseHandler<String>() {
        @Override
        public String handleResponse(final HttpResponse response) throws IOException {
            StatusLine status = response.getStatusLine();
            // status
            if (status.getStatusCode() != HttpStatus.SC_OK) {
                throw new HttpResponseException(status.getStatusCode(), status.getReasonPhrase());
            }
            // get encoding in header
            String encoding = getPageEncoding(response);
            boolean encodingFounded = true;
            if (Strings.isNullOrEmpty(encoding)) {
                encodingFounded = false;
                encoding = "iso-8859-1";
            }
            // get content and find real encoding
            HttpEntity entity = response.getEntity();
            if (entity == null) {
                return null;
            }
            // get content
            byte[] contentBytes = EntityUtils.toByteArray(entity);
            if (contentBytes == null) {
                return null;
            }
            // found encoding
            if (encodingFounded) {
                return new String(contentBytes, encoding);
            }
            // attempt to discover encoding
            String rawContent = new String(contentBytes, DEFAULT_ENCODING);
            Matcher matcher = PATTERN_HTML_CHARSET.matcher(rawContent);
            if (matcher.find()) {
                String realEncoding = matcher.group(1);
                if (!encoding.equalsIgnoreCase(realEncoding)) {
                    // bad luck :(
                    return new String(rawContent.getBytes(encoding), realEncoding);
                }
            }
            // not found right encoding :)
            return rawContent;
        }
    };
    // execute
    CloseableHttpClient client = HttpClientHolder.getClient();
    return client.execute(request, handler);
}

From source file:dataprocessing.elasticsearch.ElasticSearchClient.java

/** ***************************************************************
 * Constructor/* w  ww.  ja v  a 2s.  c  o  m*/
 */
public ElasticSearchClient() {

    ResourceBundle resourceBundle = ResourceBundle.getBundle("elasticsearch");
    index = resourceBundle.getObject("index").toString();
    type = resourceBundle.getObject("type").toString();
    header = new BasicHeader("CloudMinds", "empty");

    client = RestClient.builder(new HttpHost(resourceBundle.getObject("host").toString(),
            Integer.parseInt(resourceBundle.getObject("port").toString()),
            resourceBundle.getObject("protocol").toString())).build();
}

From source file:ch.cyberduck.core.http.LoggingHttpRequestExecutor.java

@Override
public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn,
        final HttpContext context) throws IOException, HttpException {
    if (!request.containsHeader(HttpHeaders.USER_AGENT)) {
        request.addHeader(new BasicHeader(HttpHeaders.USER_AGENT, useragentProvider.get()));
    }//from   w  w w .  j  av a  2 s . co m
    return super.execute(request, conn, context);
}

From source file:com.mcxiaoke.next.http.entity.mime.MultipartFormEntity.java

MultipartFormEntity(final AbstractMultipartForm multipart, final String contentType, final long contentLength) {
    super();/* w ww . j  a  v a  2s  .c  om*/
    this.multipart = multipart;
    this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType);
    this.contentLength = contentLength;
}

From source file:org.springframework.cloud.netflix.ribbon.apache.HttpClientStatusCodeExceptionTest.java

@Test
public void getResponse() throws Exception {
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    doReturn(new Locale("en")).when(response).getLocale();
    Header foo = new BasicHeader("foo", "bar");
    Header[] headers = new Header[] { foo };
    doReturn(headers).when(response).getAllHeaders();
    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "Success");
    doReturn(statusLine).when(response).getStatusLine();
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream("foo".getBytes()));
    entity.setContentLength(3);//ww w  . j a  va 2 s. co  m
    doReturn(entity).when(response).getEntity();
    HttpEntity copiedEntity = HttpClientUtils.createEntity(response);
    HttpClientStatusCodeException ex = new HttpClientStatusCodeException("service", response, copiedEntity,
            new URI("http://service.com"));
    assertEquals("en", ex.getResponse().getLocale().toString());
    assertArrayEquals(headers, ex.getResponse().getAllHeaders());
    assertEquals("Success", ex.getResponse().getStatusLine().getReasonPhrase());
    assertEquals(200, ex.getResponse().getStatusLine().getStatusCode());
    assertEquals("http", ex.getResponse().getStatusLine().getProtocolVersion().getProtocol());
    assertEquals(1, ex.getResponse().getStatusLine().getProtocolVersion().getMajor());
    assertEquals(1, ex.getResponse().getStatusLine().getProtocolVersion().getMinor());
    assertEquals("foo", EntityUtils.toString(ex.getResponse().getEntity()));
    verify(response, times(1)).close();
}

From source file:com.uber.jaeger.httpclient.ClientRequestCarrier.java

@Override
public void put(String key, String value) {
    request.addHeader(new BasicHeader(key, value));
}

From source file:org.fiware.apps.repository.it.collectionService.CollectionServicePostITTest.java

@BeforeClass
public static void setUpClass() throws IOException {
    IntegrationTestHelper client = new IntegrationTestHelper();
    List<Header> headers = new LinkedList<>();
    headers.add(new BasicHeader("Content-Type", "application/json"));

    //Delete the collection
    client.deleteCollection(collection, headers);
}

From source file:com.indoqa.httpproxy.HttpProxyBuilder.java

public HttpProxyBuilder addDefaultHeader(String name, String value) {
    this.defaultHeaders.add(new BasicHeader(name, value));
    return this;
}

From source file:com.anjlab.logback.hipchat.HipChatRoom.java

public HipChatRoom(String room, String apiKey) {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    closeableHttpClient = HttpClients.custom().setConnectionManager(cm).build();

    gson = new Gson();

    roomUri = "https://api.hipchat.com/v2/room/" + room + "/notification";
    authorizationHeader = new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);
    contentTypeHeader = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
}

From source file:ResourceClients.BuyerResource.java

/**
 * Resgata o comprador//  w w  w.j  av  a 2s  .  c  o m
 * @param buyerKey
 * @return
 * @throws Exception 
 */
public HttpResponseGenericResponse<GetBuyerDataResponse> GetBuyer(UUID buyerKey) throws Exception {

    HttpVerbEnum httpVerb = HttpVerbEnum.Get;

    BasicHeader[] header = new BasicHeader[1];
    header[0] = new BasicHeader("MerchantKey", this.getMerchantKey().toString());

    String serviceUri = this.getHostUri() + this.getResourceName() + buyerKey.toString();

    return this.getHttpUtility().<GetBuyerDataResponse>SubmitRequest(GetBuyerDataResponse.class, serviceUri,
            httpVerb, this.getHttpContentType(), header);
}