List of usage examples for org.apache.http.impl.client DefaultHttpClient getParams
public synchronized final HttpParams getParams()
From source file:com.ibm.watson.developer_cloud.service.WatsonService.java
/** * Gets the thread safe client./*from w w w .j a v a 2s.c o m*/ * * @return the thread safe client */ private HttpClient getThreadSafeClient() { DefaultHttpClient client = new DefaultHttpClient(getDefaultRequestParams()); ClientConnectionManager mgr = client.getConnectionManager(); HttpParams params = client.getParams(); client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params); return client; }
From source file:org.trancecode.xproc.step.HttpRequestStepProcessor.java
private HttpClient prepareHttpClient(final XProcHttpRequest xProcRequest, final BasicHttpContext localContext) { final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(schemeRegistry); final DefaultHttpClient httpClient = new DefaultHttpClient(connManager); final ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); httpClient.setRoutePlanner(routePlanner); if (xProcRequest.getCredentials() != null) { final List<String> authPref = Lists.newArrayList(AuthPolicy.BASIC, AuthPolicy.DIGEST); httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authPref); httpClient.setCredentialsProvider(xProcRequest.getCredentials()); final AuthCache authCache = new BasicAuthCache(); final BasicScheme basicAuth = new BasicScheme(); authCache.put(xProcRequest.getHttpHost(), basicAuth); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); }//from w w w . j av a 2 s . co m return httpClient; }
From source file:password.pwm.http.client.PwmHttpClient.java
public static HttpClient getHttpClient(final Configuration configuration, final PwmHttpClientConfiguration pwmHttpClientConfiguration) throws PwmUnrecoverableException { final DefaultHttpClient httpClient; try {/* w w w. j a va2 s .c o m*/ if (Boolean.parseBoolean(configuration.readAppProperty(AppProperty.SECURITY_HTTP_PROMISCUOUS_ENABLE))) { httpClient = new DefaultHttpClient(makeConnectionManager(new X509Utils.PromiscuousTrustManager())); } else if (pwmHttpClientConfiguration != null && pwmHttpClientConfiguration.getCertificates() != null) { final TrustManager trustManager = new X509Utils.CertMatchingTrustManager(configuration, pwmHttpClientConfiguration.getCertificates()); httpClient = new DefaultHttpClient(makeConnectionManager(trustManager)); } else { httpClient = new DefaultHttpClient(); } } catch (Exception e) { throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "unexpected error creating promiscuous https client: " + e.getMessage())); } final String strValue = configuration.readSettingAsString(PwmSetting.HTTP_PROXY_URL); if (strValue != null && strValue.length() > 0) { final URI proxyURI = URI.create(strValue); final String host = proxyURI.getHost(); final int port = proxyURI.getPort(); final HttpHost proxy = new HttpHost(host, port); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); final String username = proxyURI.getUserInfo(); if (username != null && username.length() > 0) { final String password = (username.contains(":")) ? username.split(":")[1] : ""; final UsernamePasswordCredentials passwordCredentials = new UsernamePasswordCredentials(username, password); httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), passwordCredentials); } } final String userAgent = PwmConstants.PWM_APP_NAME + " " + PwmConstants.SERVLET_VERSION; httpClient.getParams().setParameter(HttpProtocolParams.USER_AGENT, userAgent); return httpClient; }
From source file:com.amazon.s3.http.HttpClientFactory.java
/** * Creates a new HttpClient object using the specified AWS * ClientConfiguration to configure the client. *// w ww . ja v a2 s . com * @param config * Client configuration options (ex: proxy settings, connection * limits, etc). * * @return The new, configured HttpClient. */ public HttpClient createHttpClient(ClientConfiguration config) { /* Form User-Agent information */ String userAgent = config.getUserAgent(); if (!(userAgent.equals(ClientConfiguration.DEFAULT_USER_AGENT))) { userAgent += ", " + ClientConfiguration.DEFAULT_USER_AGENT; } /* Set HTTP client parameters */ HttpParams httpClientParams = new BasicHttpParams(); HttpClientParams.setRedirecting(httpClientParams, false); HttpProtocolParams.setUserAgent(httpClientParams, userAgent); HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout()); HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, false); HttpConnectionParams.setTcpNoDelay(httpClientParams, true); int socketSendBufferSizeHint = config.getSocketBufferSizeHints()[0]; int socketReceiveBufferSizeHint = config.getSocketBufferSizeHints()[1]; if (socketSendBufferSizeHint > 0 || socketReceiveBufferSizeHint > 0) { HttpConnectionParams.setSocketBufferSize(httpClientParams, Math.max(socketSendBufferSizeHint, socketReceiveBufferSizeHint)); } /* Set connection manager */ ThreadSafeClientConnManager connectionManager = ConnectionManagerFactory .createThreadSafeClientConnManager(config, httpClientParams); DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, httpClientParams); /* Set proxy if configured */ String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { Log.i(TAG, "Configuring Proxy. Proxy Host: " + proxyHost + " " + "Proxy Port: " + proxyPort); HttpHost proxyHttpHost = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost); String proxyUsername = config.getProxyUsername(); String proxyPassword = config.getProxyPassword(); String proxyDomain = config.getProxyDomain(); String proxyWorkstation = config.getProxyWorkstation(); if (proxyUsername != null && proxyPassword != null) { httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain)); } } return httpClient; }
From source file:net.networksaremadeofstring.rhybudd.ZenossAPI.java
public static boolean registerPushKey(String PushKey, String GCMID, String DeviceID) { DefaultHttpClient client = new DefaultHttpClient(); SchemeRegistry registry = new SchemeRegistry(); SocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); registry.register(new Scheme("https", socketFactory, 443)); ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(client.getParams(), registry); DefaultHttpClient httpclient = new DefaultHttpClient(mgr, client.getParams()); HttpPost httpost = new HttpPost("https://api.coldstart.io/1/updaterhybuddpushkey"); //httpost.addHeader("Content-type", "application/json; charset=utf-8"); httpost.setHeader("Accept", "application/json"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("pushkey", PushKey)); nvps.add(new BasicNameValuePair("gcmid", GCMID)); nvps.add(new BasicNameValuePair("deviceid", DeviceID)); JSONObject json;/*w w w.j a v a2s .c om*/ try { httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpost); String rawJSON = EntityUtils.toString(response.getEntity()); response.getEntity().consumeContent(); //Log.e("rawJSON",rawJSON); json = new JSONObject(rawJSON); } catch (Exception e) { e.printStackTrace(); return false; } if (json.has("uuid") && json.has("success")) { Boolean success; try { success = json.getBoolean("success"); } catch (Exception e) { success = false; } if (success) { return true; } else { return false; } } else { return false; } }
From source file:com.meh.IceProxy1.java
private HttpResponse download(String url) { DefaultHttpClient seed = new DefaultHttpClient(); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); SingleClientConnManager mgr = new MyClientConnManager(seed.getParams(), registry); DefaultHttpClient http = new DefaultHttpClient(mgr, seed.getParams()); HttpGet method = new HttpGet(url); method.addHeader("Icy-MetaData", "1"); HttpResponse response = null;/*from ww w. j av a 2 s .c o m*/ try { Log.d(tag, "starting download"); response = http.execute(method); Log.d(tag, "downloaded"); } catch (ClientProtocolException e) { Log.e(tag, "Error downloading", e); } catch (IOException e) { Log.e(tag, "Error downloading", e); } return response; }
From source file:jetbrains.buildServer.commitPublisher.github.api.impl.HttpClientWrapperImpl.java
private void setupProxy(DefaultHttpClient httpclient) { final String httpProxy = TeamCityProperties.getProperty("teamcity.github.http.proxy.host"); if (StringUtil.isEmptyOrSpaces(httpProxy)) return;/* ww w. j a va2 s . c o m*/ final int httpProxyPort = TeamCityProperties.getInteger("teamcity.github.http.proxy.port", -1); if (httpProxyPort <= 0) return; LOG.info("TeamCity.GitHub will use proxy: " + httpProxy + ", port " + httpProxyPort); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(httpProxy, httpProxyPort)); final String httpProxyUser = TeamCityProperties.getProperty("teamcity.github.http.proxy.user"); final String httpProxyPassword = TeamCityProperties.getProperty("teamcity.github.http.proxy.password"); final String httpProxyDomain = TeamCityProperties.getProperty("teamcity.github.http.proxy.domain"); final String httpProxyWorkstation = TeamCityProperties .getProperty("teamcity.github.http.proxy.workstation"); if (StringUtil.isEmptyOrSpaces(httpProxyUser) || StringUtil.isEmptyOrSpaces(httpProxyPassword)) return; final Credentials creds; if (StringUtil.isEmptyOrSpaces(httpProxyDomain) || StringUtil.isEmptyOrSpaces(httpProxyWorkstation)) { LOG.info("TeamCity.GitHub will use proxy credentials: " + httpProxyUser); creds = new UsernamePasswordCredentials(httpProxyUser, httpProxyPassword); } else { LOG.info("TeamCity.GitHub will use proxy NT credentials: " + httpProxyDomain + "/" + httpProxyUser); creds = new NTCredentials(httpProxyUser, httpProxyPassword, httpProxyWorkstation, httpProxyDomain); } httpclient.getCredentialsProvider().setCredentials(new AuthScope(httpProxy, httpProxyPort), creds); }
From source file:com.openmeap.http.HttpRequestExecuterImpl.java
protected void setProxy(DefaultHttpClient httpclient, String proxyHost, Integer proxyPort, String proxyUser, String proxyPassword) {/* www . j av a 2 s .c o m*/ if (proxyUser != null) { httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); } HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); }
From source file:com.ibm.sbt.services.endpoints.FormEndpoint.java
public boolean login(String user, String password) throws AuthenticationException { boolean validAuthentication = false; String requestUrl = getUrl(); setUser(user);/*from www . j ava 2 s. c o m*/ setPassword(password); try { if (!(getLoginFormUrl().startsWith("/"))) { requestUrl = requestUrl.concat("/"); } requestUrl = requestUrl.concat(getLoginFormUrl()); BasicCookieStore cookieStore = new BasicCookieStore(); DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); if (isForceTrustSSLCertificate()) { defaultHttpClient = SSLUtil.wrapHttpClient(defaultHttpClient); // Configure httpclient to accept all SSL certificates } if (isForceDisableExpectedContinue()) { defaultHttpClient.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false); } if (StringUtil.isNotEmpty(getHttpProxy())) { defaultHttpClient = ProxyDebugUtil.wrapHttpClient(defaultHttpClient, getHttpProxy()); // Configure httpclient to direct all traffic through proxy clients } defaultHttpClient.setCookieStore(cookieStore); HttpPost httpost = new HttpPost(requestUrl); List<NameValuePair> formParams = getLoginFormParameters(); // retrieve platform specific login parameters httpost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8")); //getting to interface to avoid //java.lang.NoSuchMethodError: org/apache/http/impl/client/DefaultHttpClient.execute(Lorg/apache/http/client/methods/HttpUriRequest;)Lorg/apache/http/client/methods/CloseableHttpResponse; //when run from different version of HttpClient (that's why it is deprecated) HttpClient httpClient = defaultHttpClient; HttpResponse resp = httpClient.execute(httpost); int code = resp.getStatusLine().getStatusCode(); if (code == HttpStatus.SC_OK) { validAuthentication = true; } List<Cookie> cookies = cookieStore.getCookies(); setCookieCache(cookies); } catch (IOException e) { throw new AuthenticationException(e, "FormEndpoint failed to authenticate"); } return validAuthentication; }
From source file:monakhv.samlib.http.HttpClientController.java
/** * Very row method to make http connection and begin download data Call only * by _getURL/*from ww w .j a va 2 s . c o m*/ * * @param url URL to download * @param reader File to download to can be null * @return Download data if "f" is null * @throws IOException connection problem * @throws SamLibIsBusyException host return 503 status * @throws SamlibParseException host return status other then 200 and 503 */ private String __getURL(URL url, PageReader reader) throws IOException, SamLibIsBusyException, SamlibParseException { HttpGet method = new HttpGet(url.toString()); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT); DefaultHttpClient httpclient = new DefaultHttpClient(httpParams); if (pwd != null && scope != null) { httpclient.getCredentialsProvider().setCredentials(scope, pwd); } if (proxy != null) { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } method.setHeader("User-Agent", USER_AGENT); method.setHeader("Accept-Charset", ENCODING); HttpResponse response; try { response = httpclient.execute(method); Log.d(DEBUG_TAG, "Status Response: " + response.getStatusLine().toString()); } catch (NullPointerException ex) { Log.e(DEBUG_TAG, "Connection Error", ex); throw new IOException("Connection error: " + url.toString()); } int status = response.getStatusLine().getStatusCode(); if (status == 503) { httpclient.getConnectionManager().shutdown(); throw new SamLibIsBusyException("Need to retryException "); } if (status != 200) { httpclient.getConnectionManager().shutdown(); throw new SamlibParseException("Status code: " + status); } String result = reader.doReadPage(response.getEntity().getContent()); httpclient.getConnectionManager().shutdown(); return result; }