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

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

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.quix.aia.cn.imo.mapper.UrlForUserData.java

public static void main(String[] args) {
    // TODO Auto-generated method stub
    UserAuthResponds userAuth = new UserAuthResponds();
    try {//from  w  w  w  . j av a2 s . com

        GsonBuilder builder = new GsonBuilder();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String username = "", psw = "", co = "";
        username = "NSNP306";
        psw = "A111111A";
        co = "0986";

        HttpGet getRequest = new HttpGet("http://211.144.219.243/isp/rest/index.do?isAjax=true&account="
                + username + "&co=" + co + "&password=" + psw + "");
        getRequest.addHeader("accept", "application/json");
        HttpResponse response = httpClient.execute(getRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

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

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
            Gson googleJson = new Gson();
            userAuth = googleJson.fromJson(output, UserAuthResponds.class);
            System.out.println("Success " + userAuth.getSuccess());
            if (userAuth.getSuccess().equals("1")) {
                System.out.println("Login successfully Done");
            } else {

                System.out.println("Login Failed ");
            }

        }
        httpClient.getConnectionManager().shutdown();

        //           googleJson = builder.create();
        //           Type listType = new TypeToken<List<UserAuthResponds>>() {}.getType();

    } catch (ClientProtocolException e) {
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
    }

}

From source file:es.deustotech.piramide.utils.net.RestClient.java

public static HttpEntity connect(String url, HttpEntity httpEntity) {
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = new BasicHttpParams();
    params.setParameter("http.protocol.content-charset", "UTF-8");
    httpClient.setParams(params);/*from  ww  w .j av  a  2 s  .  c  o m*/
    final HttpGet httpGet = new HttpGet(url); //request object
    HttpResponse response = null;

    try {
        response = httpClient.execute(httpGet);
    } catch (ClientProtocolException cpe) {
        Log.d(Constants.TAG, cpe.getMessage());
    } catch (IOException ioe) {
        Log.d(Constants.TAG, ioe.getMessage());
    }
    return httpEntity = response.getEntity();
}

From source file:com.Kuesty.services.JSONRequest.java

public static void execute(String uri, JSONRequestTaskHandler rsh) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;//from ww w.  j av  a2s  .c o m
    String responseString = null;

    try {
        response = httpclient.execute(new HttpGet(uri));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();

        } else {
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        rsh.onError(e.getMessage());
    } catch (IOException e) {
        rsh.onError(e.getMessage());
    }
    try {
        JSONObject response1 = new JSONObject(responseString);
        rsh.onSuccess(response1);

    } catch (JSONException e) {
        rsh.onError(e.getMessage());
    }
}

From source file:com.datatorrent.demos.mapreduce.Util.java

public static String getJsonForURL(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    logger.debug(url);//from w  w w .ja  va  2  s.com
    try {

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody;
        try {
            responseBody = httpclient.execute(httpget, responseHandler);

        } catch (ClientProtocolException e) {
            logger.debug(e.getMessage());
            return null;

        } catch (IOException e) {
            logger.debug(e.getMessage());
            return null;
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return null;
        }
        return responseBody.trim();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.openinfinity.cloud.util.http.HttpHelper.java

public static String executeHttpRequest(HttpClient client, String url) {
    HttpUriRequest request = new HttpGet(url);

    try {//w  ww  .ja  va2 s .  c o m
        HttpResponse response = client.execute(request);
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity entity = response.getEntity();
            return entity != null ? EntityUtils.toString(entity) : null;
        }
    } catch (ClientProtocolException e) {
        LOG.warn(EXCEPTION_WHILE_EXECUTING_HTTP_REQUEST + "----" + e.getMessage());
    } catch (IOException e) {
        LOG.warn(EXCEPTION_WHILE_EXECUTING_HTTP_REQUEST + "----" + e.getMessage());
    } catch (Exception e) {
        LOG.warn(EXCEPTION_WHILE_EXECUTING_HTTP_REQUEST + "----" + e.getMessage());
    }
    return null;
}

From source file:com.datatorrent.demos.mrmonitor.MRUtil.java

/**
 * This method returns the response content for a given url
 * @param url/*from w  w  w.  j av  a 2 s .  c o  m*/
 * @return
 */
public static String getJsonForURL(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    logger.debug(url);
    try {

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody;
        try {
            responseBody = httpclient.execute(httpget, responseHandler);

        } catch (ClientProtocolException e) {
            logger.debug(e.getMessage());
            return null;

        } catch (IOException e) {
            logger.debug(e.getMessage());
            return null;
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return null;
        }
        return responseBody.trim();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

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

/**
 * get request//  w w  w .ja  v a  2  s  .c om
 * @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:Main.java

public static final String postData(String url, HashMap<String, String> params) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    String outputString = null;//from   w ww . j av a2 s . c  o  m

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
        Iterator it = params.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            nameValuePairs.add(new BasicNameValuePair((String) pair.getKey(), (String) pair.getValue()));
            System.out.println(pair.getKey() + " = " + pair.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
            byte[] result = EntityUtils.toByteArray(response.getEntity());
            outputString = new String(result, "UTF-8");
        }

    } catch (ClientProtocolException e) {
        Log.i(CLASS_NAME, "Client protocolException happened: " + e.getMessage());
    } catch (IOException e) {
        Log.i(CLASS_NAME, "Client IOException happened: " + e.getMessage());
    } catch (NetworkOnMainThreadException e) {
        Log.i(CLASS_NAME, "Client NetworkOnMainThreadException happened: " + e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        Log.i(CLASS_NAME, "Unknown exeception: " + e.getMessage());
    }
    return outputString;
}

From source file:org.codehaus.mojo.license.utils.HttpRequester.java

/**
 * this method will send a simple GET-request to the given destination and will return the result as a
 * string/* w  w  w.j av  a 2  s  .c om*/
 * 
 * @param url the resource destination that is expected to contain pure text
 * @return the string representation of the resource at the given URL
 */
public static String getFromUrl(String url) throws MojoExecutionException {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet get = new HttpGet(url);
    CloseableHttpResponse response = null;

    String result = null;
    try {
        response = httpClient.execute(get);
        result = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
    } catch (ClientProtocolException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }
    return result;
}

From source file:de.akquinet.android.androlog.reporter.PostReporter.java

public static void postReport(URL url, String param) throws IOException {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url.toExternalForm());

    try {/*w w w  .j  a va 2 s  .  c  o m*/
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("report", param));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        throw new IOException(e.getMessage());
    }
}