Example usage for org.apache.http.impl.client HttpClientBuilder build

List of usage examples for org.apache.http.impl.client HttpClientBuilder build

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder build.

Prototype

public CloseableHttpClient build() 

Source Link

Usage

From source file:uk.org.openeyes.diagnostics.HttpTransfer.java

/**
 * //from w ww  .j ava2 s. c  o  m
 * @param resourceType
 * @param jsonType
 * @param requestParams
 * @param username
 * @param password
 * @return
 * @throws ConnectException 
 */
public int read(String resourceType, String jsonType, String requestParams, String username, String password)
        throws ConnectException {
    DefaultHttpClient http = new DefaultHttpClient();

    int result = -1;
    String strURL = "http://" + host + ":" + port + "/api/" + resourceType
            + "?resource_type=Patient&_format=xml";
    if (requestParams != null) {
        strURL += "&" + requestParams;
    }
    HttpGet get = new HttpGet(strURL);
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
    get.addHeader(BasicScheme.authenticate(creds, "US-ASCII", false));

    try {
        get.addHeader("Content-type", "text/xml");
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient httpclient = builder.build();

        CloseableHttpResponse httpResponse = httpclient.execute(get);
        result = httpResponse.getStatusLine().getStatusCode();
        HttpEntity entity2 = httpResponse.getEntity();
        StringWriter writer = new StringWriter();
        IOUtils.copy(entity2.getContent(), writer);
        this.response = writer.toString();
        EntityUtils.consume(entity2);
    } catch (ConnectException e) {
        // this happens when there's no server to connect to
        e.printStackTrace();
        throw e;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        get.releaseConnection();
    }
    return result;
}