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.winardiaris.uangku.getDataURL.java

String getData(String url) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from w ww  .  jav  a 2 s.c  om
        HttpGet httpget = new HttpGet(url);
        System.out.println("Executing request " + httpget.getRequestLine());

        ResponseHandler<String> responseHandler;
        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();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };

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

From source file:com.qwazr.utils.http.HttpUtils.java

/**
 * Check if the statusLine code returned the expected status code.
 *
 * @param response      The response to evaluate
 * @param expectedCodes An array of status code
 * @return the returned code//from w ww  . j a  v a2s. c  om
 * @throws ClientProtocolException if the response is empty or the status line is empty
 */
public static Integer checkStatusCodes(HttpResponse response, int... expectedCodes)
        throws ClientProtocolException {
    if (response == null)
        throw new ClientProtocolException("No response");
    StatusLine statusLine = response.getStatusLine();
    if (statusLine == null)
        throw new ClientProtocolException("Response does not contains any status");
    int statusCode = statusLine.getStatusCode();
    if (expectedCodes == null)
        return null;
    for (int code : expectedCodes)
        if (code == statusCode)
            return code;
    throw new HttpResponseEntityException(response,
            StringUtils.fastConcat("Unexpected HTTP status code: ", statusCode));
}

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

/**
 * always return null./*w  ww.j  av  a2s .  com*/
 */
@Override
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

    Args.notNull(out, "OutputStream");

    int status = response.getStatusLine().getStatusCode();

    if (status != 200) {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }

    HttpEntity entity = response.getEntity();

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

    InputStream inputStream = entity.getContent();

    try {

        byte[] buff = new byte[4096];
        int l = -1;
        while ((l = inputStream.read(buff)) != -1) {

            out.write(buff, 0, l);
        }

        out.flush();

        return null;
    } finally {
        try {
            out.close();
        } catch (IOException e) {
        }
    }
}

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

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

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

    HttpEntity entity = response.getEntity();

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

    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    dbfac.setIgnoringElementContentWhitespace(true);
    dbfac.setCoalescing(true);
    dbfac.setIgnoringComments(true);
    try {
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        ContentType contentType = ContentType.getOrDefault(entity);
        //            if (!contentType.equals(ContentType.APPLICATION_XML)) {
        //                throw new ClientProtocolException("Unexpected content type:" +
        //                    contentType);
        //            }
        Charset charset = contentType.getCharset();
        if (charset == null) {
            charset = Consts.UTF_8;
        }
        return XmlObject.fromDocument(docBuilder.parse(entity.getContent(), charset.name()));
    } catch (ParserConfigurationException ex) {
        throw new IllegalStateException(ex);
    } catch (SAXException ex) {
        throw new ClientProtocolException("Malformed XML document", ex);
    }
}

From source file:org.eclipse.emf.ecp.emf2web.json.JSONResponseHandler.java

@Override
public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    final StatusLine statusLine = response.getStatusLine();

    final HttpEntity entity = response.getEntity();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }/*from w ww . ja v a2 s  .co m*/
    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }

    final Gson gson = new GsonBuilder().create();
    final ContentType contentType = ContentType.getOrDefault(entity);
    final Charset charset = contentType.getCharset();

    final Reader reader = new InputStreamReader(entity.getContent(), charset);

    return gson.fromJson(reader, new TypeToken<T>() {
    }.getType());
}

From source file:com.kingmed.dp.ndp.NDPConnectionManager.java

@Override
public void connect() throws Exception {
    String signinUrl = ndpServe.getUrlSignin();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from  www  .j a va2  s .  c o  m
        HttpGet httpget = new HttpGet(signinUrl);
        String responseBody = httpclient.execute(httpget, new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse hr) throws ClientProtocolException, IOException {
                int status = hr.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = hr.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }

        });
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        httpclient.close();
    }
}

From source file:org.jwifisd.httpclient.StringResponseHandler.java

@Override
public String handleResponse(HttpResponse response) throws IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= ByteResponseHandler.START_HTTP_OK_RESPONSE_RANGE
            && status < ByteResponseHandler.END_HTTP_OK_RESPONSE_RANGE) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {/* w w w  .  j  a va  2  s  .  c  o m*/
        throw new ClientProtocolException("Unexpected response status: " + status);
    }
}

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

/**
 * Perform post request.//from w  ww  . j  a v a2 s .com
 * @param httpPost 
 */
public static void httpPostRequest(HttpPost httpPost) {
    /* 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(httpPost, responseHandler);
        /* Print the response */
        System.out.println("Response: " + strResponse);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    int status = response.getStatusLine().getStatusCode();
    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        Charset charset = ContentType.getOrDefault(entity).getCharset();
        if (charset == null) {
            charset = Consts.UTF_8;/*from w  w  w  .  j av  a  2 s  .com*/
        }
        return entity != null ? EntityUtils.toString(entity, charset) : "";
    } else {
        throw new ClientProtocolException("Unexpected response status: " + status);
    }
}

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

@Override
public File 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.ja  va 2  s  .  c o  m*/

    HttpEntity entity = response.getEntity();

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

    File tempFile = null;

    if (this.file != null) {
        tempFile = this.file;
    } else if (this.dir != null) {
        Header contentDisposition = response.getLastHeader("Content-disposition");
        String filename = contentDisposition.getValue().split(";")[1].split("=")[1].replace("\"", "");
        tempFile = new File(this.dir, filename);
    }

    Args.notNull(tempFile, "file");
    Args.check(tempFile.canWrite(), "file must be writeable");

    InputStream inputStream = entity.getContent();
    FileOutputStream outputStream = new FileOutputStream(tempFile);

    try {

        byte[] buff = new byte[4096];
        int size = -1;
        while ((size = inputStream.read(buff)) != -1) {

            outputStream.write(buff, 0, size);
        }

        outputStream.flush();

        return tempFile;
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {
        }
    }

}