List of usage examples for org.apache.http.client ClientProtocolException getCause
public synchronized Throwable getCause()
From source file:org.ubicompforall.cityexplorer.buildingblock.BusTimeStep.java
public static String connect(String url) { String result = ""; HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); // Execute the request HttpResponse response;//from w ww . j ava2 s. c om try { response = httpclient.execute(httpget); // Examine the response status debug(2, response.getStatusLine().toString()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release if (entity != null) { // A Simple JSON Response Read InputStream instream = entity.getContent(); result = convertStreamToString(instream); // now you have the string representation of the HTML request instream.close(); } } catch (ClientProtocolException e) { debug(-1, e.getCause() + " " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { debug(-1, e.getCause() + " " + e.getMessage()); } return result; }
From source file:br.com.cams7.siscom.member.MemberList.java
private static String GET(String url, String errorMsg) { // create HttpClient HttpClient httpclient = new DefaultHttpClient(); try {// ww w .j a v a 2 s .c o m // make GET request to the given URL HttpResponse httpResponse = httpclient.execute(new HttpGet(url)); // receive response as inputStream InputStream inputStream = httpResponse.getEntity().getContent(); if (inputStream == null) { Log.d(RestUtil.TAG_INPUT_STREAM, "InputStream is null"); return errorMsg; } return RestUtil.convertInputStreamToString(inputStream); } catch (ClientProtocolException e) { Log.d(RestUtil.TAG_INPUT_STREAM, e.getLocalizedMessage(), e.getCause()); } catch (IOException e) { Log.d(RestUtil.TAG_INPUT_STREAM, e.getLocalizedMessage(), e.getCause()); } return errorMsg; }
From source file:net.shibboleth.idp.cas.authn.PkixProxyAuthenticator.java
@Override protected int authenticateProxyCallback(final URI callbackUri) throws GeneralSecurityException { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try {// w ww .ja v a 2 s. co m httpClient = createHttpClient(); log.debug("Attempting to connect to {}", callbackUri); final HttpGet request = new HttpGet(callbackUri); request.setConfig(RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build()); response = httpClient.execute(request); return response.getStatusLine().getStatusCode(); } catch (ClientProtocolException e) { throw new RuntimeException("HTTP protocol error", e); } catch (SSLException e) { if (e.getCause() instanceof CertificateException) { throw (CertificateException) e.getCause(); } throw new GeneralSecurityException("SSL connection error", e); } catch (IOException e) { throw new RuntimeException("IO error", e); } finally { close(response); close(httpClient); } }
From source file:net.shibboleth.idp.cas.proxy.impl.HttpClientProxyAuthenticator.java
@Override protected int authenticateProxyCallback(@Nonnull final URI callbackUri, @Nullable final TrustEngine<? super X509Credential> x509TrustEngine) throws GeneralSecurityException { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try {// ww w. j a v a 2 s .c o m httpClient = createHttpClient(x509TrustEngine); log.debug("Attempting to connect to {}", callbackUri); final HttpGet request = new HttpGet(callbackUri); request.setConfig(RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build()); response = httpClient.execute(request); return response.getStatusLine().getStatusCode(); } catch (ClientProtocolException e) { throw new GeneralSecurityException("HTTP protocol error", e); } catch (SSLException e) { if (e.getCause() instanceof CertificateException) { throw (CertificateException) e.getCause(); } throw new GeneralSecurityException("SSL connection error", e); } catch (IOException e) { throw new GeneralSecurityException("IO error", e); } finally { close(response); close(httpClient); } }
From source file:com.liato.bankdroid.banking.banks.CSN.java
@Override public Urllib login() throws LoginException, BankException { try {/*from w ww . j a va 2 s .c o m*/ LoginPackage lp = preLogin(); response = urlopen.open(lp.getLoginTarget(), lp.getPostData()); Matcher matcher = reLoginError.matcher(response); if (matcher.find()) { throw new LoginException(Html.fromHtml(matcher.group(1)).toString().trim()); } if (!response.contains("Inloggad som")) { throw new BankException(res.getText(R.string.unable_to_login).toString()); } } catch (ClientProtocolException e) { throw new BankException("login:CPE:" + e.getCause().getMessage()); } catch (IOException e) { e.printStackTrace(); throw new BankException("login:IOE:" + e.getMessage()); } return urlopen; }
From source file:org.gradle.caching.http.internal.HttpBuildCacheService.java
@Override public void store(BuildCacheKey key, final BuildCacheEntryWriter output) throws BuildCacheException { final URI uri = root.resolve(key.getHashCode()); HttpPut httpPut = new HttpPut(uri); httpPut.addHeader(HttpHeaders.CONTENT_TYPE, BUILD_CACHE_CONTENT_TYPE); addDiagnosticHeaders(httpPut);//from ww w. j a va2s.c o m httpPut.setEntity(new AbstractHttpEntity() { @Override public boolean isRepeatable() { return false; } @Override public long getContentLength() { return output.getSize(); } @Override public InputStream getContent() throws IOException, UnsupportedOperationException { throw new UnsupportedOperationException(); } @Override public void writeTo(OutputStream outstream) throws IOException { output.writeTo(outstream); } @Override public boolean isStreaming() { return false; } }); CloseableHttpResponse response = null; try { response = httpClientHelper.performHttpRequest(httpPut); StatusLine statusLine = response.getStatusLine(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Response for PUT {}: {}", safeUri(uri), statusLine); } int statusCode = statusLine.getStatusCode(); if (!isHttpSuccess(statusCode)) { String defaultMessage = String.format("Storing entry at '%s' response status %d: %s", safeUri(uri), statusCode, statusLine.getReasonPhrase()); if (isRedirect(statusCode)) { handleRedirect(uri, response, statusCode, defaultMessage, "storing entry at"); } else { throwHttpStatusCodeException(statusCode, defaultMessage); } } } catch (ClientProtocolException e) { Throwable cause = e.getCause(); if (cause instanceof NonRepeatableRequestException) { throw wrap(cause.getCause()); } else { throw wrap(cause); } } catch (IOException e) { throw wrap(e); } finally { HttpClientUtils.closeQuietly(response); } }
From source file:de.dan_nrw.android.followerstat.dao.folllowers.TwitterFollowerDAO.java
@Override public long getFollowerCount(String userName) throws TwitterUserAccountNotFoundException, TwitterRequestException { try {//from w ww.j a v a 2 s . c om String json = this.sendRequest( URI.create(String.format("http://twitter.com/users/show/%s.json", userName)), new StringResponseHandler(), true); JSONObject jsonObject = new JSONObject(json); return jsonObject.getLong("followers_count"); } catch (ClientProtocolException ex) { throw new TwitterRequestException(ex); } catch (SecurityException ex) { throw new TwitterAuthException(ex.getCause()); } catch (IOException ex) { if (ex.getMessage().equals("404")) { throw new TwitterUserAccountNotFoundException(); } throw new TwitterRequestException(ex); } catch (JSONException ex) { throw new TwitterResponseParsingException(ex); } catch (OAuthExpectationFailedException ex) { throw new TwitterAuthException(ex); } catch (OAuthException ex) { throw new TwitterAuthException(ex); } }
From source file:de.dan_nrw.android.followerstat.dao.folllowers.TwitterFollowerDAO.java
@Override public Follower[] getFollowers(String userName) throws TwitterUserAccountNotFoundException, TwitterRequestException { try {/* w w w .j a v a2 s. c om*/ String json = this.sendRequest( URI.create(String.format("http://twitter.com/statuses/followers/%s.json", userName)), new StringResponseHandler(), true); JSONArray jsonArray = new JSONArray(json); List<Follower> followers = new ArrayList<Follower>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); followers.add(new Follower(jsonObject.getString("screen_name"), URI.create(jsonObject.getString("profile_image_url")))); } return followers.toArray(new Follower[followers.size()]); } catch (ClientProtocolException ex) { throw new TwitterRequestException(ex); } catch (SecurityException ex) { throw new TwitterAuthException(ex.getCause()); } catch (IOException ex) { if (ex.getMessage().equals("404")) { throw new TwitterUserAccountNotFoundException(); } throw new TwitterRequestException(ex); } catch (JSONException ex) { throw new TwitterResponseParsingException(ex); } catch (OAuthExpectationFailedException ex) { throw new TwitterAuthException(ex); } catch (OAuthException ex) { throw new TwitterAuthException(ex); } }
From source file:ss.udapi.sdk.services.HttpServices.java
public ServiceRequest getSession(String url, boolean compressionEnabled) throws Exception { this.compressionEnabled = compressionEnabled; List<RestItem> loginRestItems = null; ServiceRequest loginResp = new ServiceRequest(); CloseableHttpClient httpClient = null; try {//from w w w . java 2 s . c o m logger.info("Retrieving connection actions from url: " + url); // this is only to check whether the URL format is correct new URL(url); HttpGet httpGet = new HttpGet(url); if (compressionEnabled == true) { httpGet.setHeader("Accept-Encoding", "gzip"); } httpClient = HttpClients.custom().setKeepAliveStrategy(requestTimeout).build(); ResponseHandler<String> responseHandler = getResponseHandler(401); // Call the end-point using connectivity details we've prepared above // and get the list of end-points we have access to. String responseBody = httpClient.execute(httpGet, responseHandler); loginRestItems = JsonHelper.toRestItems(responseBody); ArrayList<String> names = new ArrayList<String>(); for (RestItem item : loginRestItems) { names.add(item.getName()); } logger.debug("Retrieved connection actions: " + names.toString()); loginResp.setServiceRestItems(loginRestItems); return loginResp; } catch (MalformedURLException urlEx) { logger.error("malformed URL: " + url); throw urlEx; } catch (ClientProtocolException protEx) { logger.error("Invalid Client Protocol: " + protEx.getCause()); throw protEx; } catch (IOException ioEx) { logger.error("Communication error: to URL [" + url + "]"); throw ioEx; } finally { try { if (httpClient != null) httpClient.close(); } catch (IOException ex) { // Can safely be ignored, either the server closed the // connection or we didn't open it so there's nothing to do. } } }
From source file:com.prasanna.android.http.SecureHttpHelper.java
private <T> T executeRequest(HttpClient client, HttpRequestBase request, HttpResponseBodyParser<T> responseBodyParser) { LogWrapper.d(TAG, "HTTP request to: " + request.getURI().toString()); try {//from w ww .ja v a 2s . c o m HttpResponse httpResponse = client.execute(request); HttpEntity entity = httpResponse.getEntity(); String responseBody = EntityUtils.toString(entity, HTTP.UTF_8); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) return responseBodyParser.parse(responseBody); else { LogWrapper.d(TAG, "Http request failed: " + statusCode + ", " + responseBody); if (statusCode >= HttpErrorFamily.SERVER_ERROR) throw new ServerException(statusCode, httpResponse.getStatusLine().getReasonPhrase(), responseBody); else if (statusCode >= HttpErrorFamily.CLIENT_ERROR) throw new ClientException(statusCode, httpResponse.getStatusLine().getReasonPhrase(), responseBody); } } catch (ClientProtocolException e) { LogWrapper.e(TAG, e.getMessage()); } catch (IOException e) { LogWrapper.e(TAG, e.getMessage()); } catch (HttpResponseParseException e) { throw new ClientException(ClientErrorCode.RESPONSE_PARSE_ERROR, e.getCause()); } return null; }