List of usage examples for org.apache.http.impl.client DefaultHttpClient getParams
public synchronized final HttpParams getParams()
From source file:net.paissad.minus.utils.HttpClientUtils.java
/** * /* w ww . j ava 2s .c o m*/ * @param baseURL * @param parametersBody * @param sessionId * @param fileToUpload - The file to upload. * @param filename - The name of the file to use during the upload process. * @return The response received from the Minus API. * @throws MinusException */ public static MinusHttpResponse doUpload(final String baseURL, final Map<String, String> parametersBody, final String sessionId, final File fileToUpload, final String filename) throws MinusException { DefaultHttpClient client = null; HttpPost uploadRequest = null; InputStream responseContent = null; try { String url = baseURL; if (parametersBody != null && !parametersBody.isEmpty()) { url += CommonUtils.encodeParams(parametersBody); } uploadRequest = new HttpPost(url); client = new DefaultHttpClient(); client.setHttpRequestRetryHandler(new MinusHttpRequestRetryHandler()); FileEntity fileEntity = new FileEntity(fileToUpload, "application/octet-stream"); uploadRequest.setEntity(fileEntity); // We add this headers as specified by the Minus.com API during file // upload. uploadRequest.addHeader("Content-Disposition", "attachment; filename=a.bin"); uploadRequest.addHeader("Content-Type", "application/octet-stream"); client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true); client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); CookieStore cookieStore = new BasicCookieStore(); Cookie sessionCookie = null; if (sessionId != null && !sessionId.trim().isEmpty()) { sessionCookie = new BasicClientCookie2(MINUS_COOKIE_NAME, sessionId); ((BasicClientCookie2) sessionCookie).setPath("/"); ((BasicClientCookie2) sessionCookie).setDomain(MINUS_DOMAIN_NAME); ((BasicClientCookie2) sessionCookie).setVersion(0); cookieStore.addCookie(sessionCookie); } client.setCookieStore(cookieStore); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpResponse httpResponse = client.execute(uploadRequest); // Let's update the cookie have the name 'sessionid' for (Cookie aCookie : client.getCookieStore().getCookies()) { if (aCookie.getName().equals(MINUS_COOKIE_NAME)) { sessionCookie = aCookie; break; } } StringBuilder result = new StringBuilder(); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity respEntity = httpResponse.getEntity(); if (respEntity != null) { result.append(EntityUtils.toString(respEntity)); EntityUtils.consume(respEntity); } } else { // The response code is not OK. StringBuilder errMsg = new StringBuilder(); errMsg.append(" Upload failed => ").append(httpResponse.getStatusLine()); if (uploadRequest != null) { errMsg.append(" : ").append(uploadRequest.getURI()); } throw new MinusException(errMsg.toString()); } return new MinusHttpResponse(result.toString(), sessionCookie); } catch (Exception e) { if (uploadRequest != null) { uploadRequest.abort(); } String errMsg = "Error while uploading file (" + fileToUpload + ") : " + e.getMessage(); throw new MinusException(errMsg, e); } finally { if (client != null) { client.getConnectionManager().shutdown(); } CommonUtils.closeAllStreamsQuietly(responseContent); } }
From source file:rawsclient.RawsClient.java
@SuppressWarnings("unchecked") private Map<String, Object> exec_request(HttpRequestBase httpMethod) throws HttpResponseException, IOException, ClassCastException { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter("http.useragent", this.user_agent_name); BASE64Encoder enc = new sun.misc.BASE64Encoder(); String userpassword = this.username + ":" + this.password; String encodedAuthorization = enc.encode(userpassword.getBytes()); httpMethod.addHeader("Authorization", "Basic " + encodedAuthorization); HttpResponse response = httpClient.execute(httpMethod); if (response.getStatusLine().getStatusCode() > 299) { // try to get the reasons from the json error response String strErr = getReasonsFromErrorMsg(response); if (strErr.isEmpty()) { // if we can't get the reasons, dump the entire response body strErr = inputStreamToString(response.getEntity().getContent()).toString(); }/*from w w w . j av a 2s .c o m*/ throw new HttpResponseException(response.getStatusLine().getStatusCode(), strErr); } ObjectMapper mapper2 = new ObjectMapper(); Object responseObject = mapper2.readValue(response.getEntity().getContent(), Object.class); httpMethod.releaseConnection(); return (Map<String, Object>) responseObject; }
From source file:com.quantcast.measurement.service.QCPolicy.java
private void getPolicy(Context context) { //if we are blacked out we cant go get the policy yet if (isBlackedOut()) return;/* w w w .ja va2 s.c o m*/ boolean loadedPolicy = checkPolicy(context, false); if (!loadedPolicy) { String jsonString = null; DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); defaultHttpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent")); InputStream inputStream = null; try { HttpGet method = new HttpGet(m_policyURL); HttpResponse response = defaultHttpClient.execute(method); inputStream = response.getEntity().getContent(); jsonString = readStreamToString(inputStream); } catch (Exception e) { QCLog.e(TAG, "Could not download policy", e); QCMeasurement.INSTANCE.logSDKError("policy-download-failure", e.getMessage(), null); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ignored) { } } } if (jsonString != null) { savePolicy(context, jsonString); loadedPolicy = parsePolicy(jsonString); } } m_policyIsLoaded = loadedPolicy; }
From source file:com.norconex.collector.http.robot.impl.DefaultRobotsTxtProvider.java
@Override public synchronized RobotsTxt getRobotsTxt(DefaultHttpClient httpClient, String url) { String baseURL = getBaseURL(url); RobotsTxt robotsTxt = robotsTxtCache.get(baseURL); if (robotsTxt != null) { return robotsTxt; }//ww w.j a va 2s. com String userAgent = ((String) httpClient.getParams().getParameter(CoreProtocolPNames.USER_AGENT)) .toLowerCase(); String robotsURL = baseURL + "/robots.txt"; HttpGet method = new HttpGet(robotsURL); List<String> sitemapLocations = new ArrayList<String>(); List<IURLFilter> filters = new ArrayList<IURLFilter>(); MutableFloat crawlDelay = new MutableFloat(RobotsTxt.UNSPECIFIED_CRAWL_DELAY); try { HttpResponse response = httpClient.execute(method); InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); BufferedReader br = new BufferedReader(isr); boolean agentAlreadyMatched = false; boolean doneWithAgent = false; String line; while ((line = br.readLine()) != null) { String key = line.replaceFirst("(.*?)(:.*)", "$1").trim(); String value = line.replaceFirst("(.*?:)(.*)", "$2").trim(); if ("sitemap".equalsIgnoreCase(key)) { sitemapLocations.add(value); } if (!doneWithAgent) { if ("user-agent".equalsIgnoreCase(key)) { if (matchesUserAgent(userAgent, value)) { agentAlreadyMatched = true; } else if (agentAlreadyMatched) { doneWithAgent = true; } } if (agentAlreadyMatched) { parseAgentLines(baseURL, filters, crawlDelay, key, value); } } } isr.close(); } catch (Exception e) { LOG.warn("Not able to obtain robots.txt at: " + robotsURL, e); } robotsTxt = new RobotsTxt(filters.toArray(new IURLFilter[] {}), crawlDelay.floatValue()); robotsTxtCache.put(baseURL, robotsTxt); return robotsTxt; }
From source file:com.allblacks.utils.web.HttpUtil.java
private HttpUtil() { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpParams params = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); }
From source file:org.brussels.gtug.attendance.AccountsActivity.java
/** * Retrieves the authorization cookie associated with the given token. This * method should only be used when running against a production appengine * backend (as opposed to a dev mode server). *//*from w w w.j a v a2 s .com*/ private String getAuthCookie(String authToken) { DefaultHttpClient httpClient = new DefaultHttpClient(); try { // Get SACSID cookie httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); String uri = Constants.APP_SERVER_URL + "/_ah/login?continue=http://localhost/&auth=" + authToken; HttpGet method = new HttpGet(uri); HttpResponse res = httpClient.execute(method); StatusLine statusLine = res.getStatusLine(); int statusCode = statusLine.getStatusCode(); Header[] headers = res.getHeaders("Set-Cookie"); if (statusCode != 302 || headers.length == 0) { return null; } for (Cookie cookie : httpClient.getCookieStore().getCookies()) { if (AUTH_COOKIE_NAME.equals(cookie.getName())) { return AUTH_COOKIE_NAME + "=" + cookie.getValue(); } } } catch (IOException e) { Log.w(TAG, "Got IOException " + e); Log.w(TAG, Log.getStackTraceString(e)); } finally { httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); } return null; }
From source file:org.ancoron.osgi.test.glassfish.GlassfishDerbyTest.java
protected DefaultHttpClient getHTTPClient() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { @Override// w ww . j a v a 2 s . co m public X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers ============="); return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { System.out.println("checkClientTrusted ============="); } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { System.out.println("checkServerTrusted ============="); } } }, new SecureRandom()); SSLSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme httpsScheme = new Scheme("https", 8181, sf); PlainSocketFactory plain = new PlainSocketFactory(); Scheme httpScheme = new Scheme("http", 8080, plain); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); schemeRegistry.register(httpScheme); HttpParams params = new BasicHttpParams(); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); // Increase max total connection to 200 cm.setMaxTotal(200); // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(20); DefaultHttpClient httpClient = new DefaultHttpClient(cm, params); httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); return httpClient; }
From source file:com.groupon.jenkins.util.HttpPoster.java
public String post(String url, Map postData) throws IOException { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); try {/* w ww.j av a 2s . c om*/ post.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(postData))); HttpResponse response = httpclient.execute(post); HttpHost proxy = getProxy(post); if (proxy != null) { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException(response.getStatusLine().toString()); } return getResponse(response); } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:com.box.androidlib.BoxFileDownload.java
/** * Execute a file download./*from w ww. j a v a 2 s. c o m*/ * * @param fileId * The file_id of the file to be downloaded * @param destinationOutputStreams * OutputStreams to which the data should be written to as it is downloaded. * @param versionId * The version_id of the version of the file to download. Set to null to download the latest version of the file. * @return a response handler * @throws IOException * Can be thrown if there was a connection error, or if destination file could not be written. */ public DefaultResponseParser execute(final long fileId, final OutputStream[] destinationOutputStreams, final Long versionId) throws IOException { final DefaultResponseParser handler = new DefaultResponseParser(); final Uri.Builder builder = new Uri.Builder(); builder.scheme(BoxConfig.getInstance().getDownloadUrlScheme()); builder.encodedAuthority(BoxConfig.getInstance().getDownloadUrlAuthority()); builder.path(BoxConfig.getInstance().getDownloadUrlPath()); builder.appendPath(mAuthToken); builder.appendPath(String.valueOf(fileId)); if (versionId != null) { builder.appendPath(String.valueOf(versionId)); } List<BasicNameValuePair> customQueryParams = BoxConfig.getInstance().getCustomQueryParameters(); if (customQueryParams != null && customQueryParams.size() > 0) { for (BasicNameValuePair param : customQueryParams) { builder.appendQueryParameter(param.getName(), param.getValue()); } } // We normally prefer to use HttpUrlConnection, however that appears to fail for certain types of files. For downloads, it appears DefaultHttpClient // works more reliably. final DefaultHttpClient httpclient = new DefaultHttpClient(); HttpProtocolParams.setUserAgent(httpclient.getParams(), BoxConfig.getInstance().getUserAgent()); HttpGet httpGet; String theUri = builder.build().toString(); if (BoxConfig.getInstance().getHttpLoggingEnabled()) { //DevUtils.logcat("User-Agent : " + HttpProtocolParams.getUserAgent(httpclient.getParams())); DevUtils.logcat("Box Request: " + theUri); } try { httpGet = new HttpGet(new URI(theUri)); } catch (URISyntaxException e) { throw new IOException("Invalid Download URL"); } httpGet.setHeader("Connection", "close"); httpGet.setHeader("Accept-Language", BoxConfig.getInstance().getAcceptLanguage()); HttpResponse httpResponse = httpclient.execute(httpGet); int responseCode = httpResponse.getStatusLine().getStatusCode(); if (BoxConfig.getInstance().getHttpLoggingEnabled()) { DevUtils.logcat("Box Response: " + responseCode); // Header[] headers = httpResponse.getAllHeaders(); // for (Header header : headers) { // DevUtils.logcat("Response Header: " + header.toString()); // } } // Server returned a 503 Service Unavailable. Usually means a temporary unavailability. if (responseCode == HttpStatus.SC_SERVICE_UNAVAILABLE) { // if (BoxConfig.getInstance().getHttpLoggingEnabled()) { // DevUtils.logcat("HTTP Response Code: " + HttpStatus.SC_SERVICE_UNAVAILABLE); // } handler.setStatus(ResponseListener.STATUS_SERVICE_UNAVAILABLE); return handler; } InputStream is = httpResponse.getEntity().getContent(); if (responseCode == HttpURLConnection.HTTP_OK) { // Grab the first 100 bytes and check for an error string. // Not a good way for the server to respond with errors but that's the way it is for now. final byte[] errorCheckBuffer = new byte[FILE_ERROR_SIZE]; // Three cases here: error, no error && file size == 0, no error && file size > 0. // The first case will be handled by parsing the error check buffer. The second case will result in mBytesTransferred == 0, // no real bytes will be transferred but we still treat as success case. The third case is normal success case. mBytesTransferred = Math.max(0, is.read(errorCheckBuffer)); final String str = new String(errorCheckBuffer).trim(); if (str.equals(FileDownloadListener.STATUS_DOWNLOAD_WRONG_AUTH_TOKEN)) { handler.setStatus(FileDownloadListener.STATUS_DOWNLOAD_WRONG_AUTH_TOKEN); } else if (str.equals(FileDownloadListener.STATUS_DOWNLOAD_RESTRICTED)) { handler.setStatus(FileDownloadListener.STATUS_DOWNLOAD_RESTRICTED); } // No error detected else { // Copy the file to destination if > 0 bytes of file transferred. if (mBytesTransferred > 0) { for (int i = 0; i < destinationOutputStreams.length; i++) { destinationOutputStreams[i].write(errorCheckBuffer, 0, (int) mBytesTransferred); // Make sure we don't lose that first 100 bytes. } // Read the rest of the stream and write to the destination OutputStream. final byte[] buffer = new byte[DOWNLOAD_BUFFER_SIZE]; int bufferLength = 0; long lastOnProgressPost = 0; while (!Thread.currentThread().isInterrupted() && (bufferLength = is.read(buffer)) > 0) { for (int i = 0; i < destinationOutputStreams.length; i++) { destinationOutputStreams[i].write(buffer, 0, bufferLength); } mBytesTransferred += bufferLength; long currTime = SystemClock.uptimeMillis(); if (mListener != null && mHandler != null && currTime - lastOnProgressPost > ON_PROGRESS_UPDATE_THRESHOLD) { lastOnProgressPost = currTime; mHandler.post(mOnProgressRunnable); } } mHandler.post(mOnProgressRunnable); } for (int i = 0; i < destinationOutputStreams.length; i++) { destinationOutputStreams[i].close(); } handler.setStatus(FileDownloadListener.STATUS_DOWNLOAD_OK); // If download thread was interrupted, set to // STATUS_DOWNLOAD_CANCELED if (Thread.currentThread().isInterrupted()) { httpGet.abort(); handler.setStatus(FileDownloadListener.STATUS_DOWNLOAD_CANCELLED); } } } else if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) { handler.setStatus(FileDownloadListener.STATUS_DOWNLOAD_PERMISSIONS_ERROR); } else { handler.setStatus(FileDownloadListener.STATUS_DOWNLOAD_FAIL); } httpResponse.getEntity().consumeContent(); if (httpclient != null && httpclient.getConnectionManager() != null) { httpclient.getConnectionManager().closeIdleConnections(500, TimeUnit.MILLISECONDS); } is.close(); return handler; }