Example usage for org.apache.http.client ClientProtocolException ClientProtocolException

List of usage examples for org.apache.http.client ClientProtocolException ClientProtocolException

Introduction

In this page you can find the example usage for org.apache.http.client ClientProtocolException ClientProtocolException.

Prototype

public ClientProtocolException(final Throwable cause) 

Source Link

Usage

From source file:org.mobile.mpos.util.HttpClientHelper.java

/**
 * get request/*from  w w w  .ja  va 2s  . c o m*/
 * @param uri
 * @return
 */
public static String get(String uri) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpGet httpget = new HttpGet(uri);
        log.info("Executing request " + httpget.getRequestLine());
        // Create a custom response handler
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            public String handleResponse(final HttpResponse response)
                    throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };
        String responseBody = httpClient.execute(httpget, responseHandler);
        log.info(" response " + responseBody);
        return responseBody;
    } catch (ClientProtocolException e) {
        log.error("ClientProtocolException " + e.getMessage());
    } catch (IOException e) {
        log.error("IOException " + e.getMessage());
    } finally {
        close(httpClient);
    }
    return null;
}

From source file:com.javaquery.aws.elasticsearch.GetExample.java

/**
 * Perform get request.//from  w ww  . jav a 2 s  .co m
 * @param httpGet
 */
public static void httpGetRequest(HttpGet httpGet) {
    /* Create object of CloseableHttpClient */
    CloseableHttpClient httpClient = HttpClients.createDefault();

    /* Response handler for after request execution */
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

        @Override
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            /* Get status code */
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                /* Convert response to String */
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        }
    };

    try {
        /* Execute URL and attach after execution response handler */
        String strResponse = httpClient.execute(httpGet, responseHandler);
        /* Print the response */
        System.out.println("Response: " + strResponse);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.uber.stream.kafka.mirrormaker.common.utils.HttpClientUtils.java

public static ResponseHandler<String> createResponseBodyExtractor() {
    return new ResponseHandler<String>() {
        @Override//w w w  . j  av  a2  s. c  o  m
        public String handleResponse(final HttpResponse response) throws IOException {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException(
                        "Unexpected response status while getting /topics : " + status);
            }
        }
    };
}

From source file:com.javaquery.aws.elasticsearch.DeleteExample.java

/**
 * Perform delete request./*  w w w. j ava  2s  . c  o m*/
 * @param httpDelete
 */
public static void httpDeleteRequest(HttpDelete httpDelete) {
    /* Create object of CloseableHttpClient */
    CloseableHttpClient httpClient = HttpClients.createDefault();

    /* Response handler for after request execution */
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

        @Override
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            /* Get status code */
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                /* Convert response to String */
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        }
    };

    try {
        /* Execute URL and attach after execution response handler */
        String strResponse = httpClient.execute(httpDelete, responseHandler);
        /* Print the response */
        System.out.println("Response: " + strResponse);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.wudaosoft.net.httpclient.ImageResponseHandler.java

@Override
public BufferedImage handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();

    if (status != 200) {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }//from   w w w  .j av a2  s . c  o  m

    HttpEntity entity = response.getEntity();

    if (entity == null || !entity.isStreaming()) {
        throw new ClientProtocolException("Response contains no content");
    }

    BufferedImage buffImg = ImageIO.read(entity.getContent());
    return buffImg;
}

From source file:de.dan_nrw.web.HttpResponseHandler.java

@Override
public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    if (response.getStatusLine().getStatusCode() >= 400) {
        throw new ClientProtocolException(Integer.toString(response.getStatusLine().getStatusCode()));
    }//from  w  w  w.ja  v  a  2s  .co  m

    return this.handleResponseInternal(response);
}

From source file:org.openmrs.contrib.discohub.HttpUtils.java

public static Map<String, Object> getData(String url, Header[] headers) throws IOException {
    final Map<String, Object> responseMap = new LinkedHashMap<>();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(url);
    httpget.setHeaders(headers);//w ww. ja  v  a  2 s.  c  o  m

    // Create a custom response handler
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
        @Override
        public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                responseMap.put("headers", response.getAllHeaders());
                //System.out.println("Ratelimit attempts left: " + response.getHeaders("X-RateLimit-Remaining")[0]);
                //System.out.println("Etag = " + response.getHeaders("ETag")[0]);
                return entity != null ? EntityUtils.toString(entity) : null;
            } else if (status == 304) {
                //System.out.println("GOT 304!!!");
                return null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        }

    };
    String responseBody = httpclient.execute(httpget, responseHandler);
    responseMap.put("content", responseBody);
    return responseMap;
}

From source file:org.apache.stratos.integration.common.rest.HttpResponseHandler.java

@Override
public HttpResponse handleResponse(org.apache.http.HttpResponse response)
        throws ClientProtocolException, IOException {
    StatusLine statusLine = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }// w  ww .  j  av  a 2 s .c om

    BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

    String output;
    String result = "";

    while ((output = reader.readLine()) != null) {
        result += output;
    }

    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(statusLine.getStatusCode());
    httpResponse.setContent(result);
    httpResponse.setReason(statusLine.getReasonPhrase());

    if (log.isDebugEnabled()) {
        log.debug("Extracted Http Response: " + httpResponse.toString());
    }

    return httpResponse;
}

From source file:com.wudaosoft.net.httpclient.SAXSourceResponseHandler.java

@Override
public SAXSource handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();

    if (status < 200 || status >= 300) {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }/*from   w w w.  j ava  2s .c om*/

    HttpEntity entity = response.getEntity();

    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }

    return readSAXSource(entity.getContent());
}

From source file:com.mycompany.rakornas.getDataURL.java

String getData(String url) throws IOException {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        HttpGet httpget = new HttpGet(url);
        System.out.println("Executing request " + httpget.getRequestLine());

        ResponseHandler<String> responseHandler;
        responseHandler = new ResponseHandler<String>() {

            @Override//  w w w  .ja v a2  s .  c om
            public String handleResponse(final HttpResponse response)
                    throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };

        String responseURL = httpclient.execute(httpget, responseHandler);
        return responseURL;
    }
}