List of usage examples for org.apache.http.impl.client DefaultHttpClient getParams
public synchronized final HttpParams getParams()
From source file:jp.mixi.android.sdk.MixiContainerImpl.java
private synchronized HttpClient getHttpClient() { if (mHttpClient != null) { return mHttpClient; }//from ww w. j a va 2 s . c om DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); ClientConnectionManager mgr = defaultHttpClient.getConnectionManager(); HttpParams params = defaultHttpClient.getParams(); mHttpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params); mHttpClient.getParams().setParameter(HTTP_PARAM_CONNECTION_TIMEOUT, mConnectionTimeout); mHttpClient.getParams().setParameter(HTTP_PARAM_SOCKET_TIMEOUT, mSocketTimeout); return mHttpClient; }
From source file:fr.paris.lutece.plugins.mylutece.modules.oauth.authentication.OAuthAuthentication.java
/** * Builds a new {@link HttpClient}/*from w w w . j a v a2s . c om*/ * @return new HttpClient */ private HttpClient getHttpClient() { DefaultHttpClient client = new DefaultHttpClient(); String strUserName = AppPropertiesService.getProperty(PROPERTY_PROXY_USERNAME); String strPassword = AppPropertiesService.getProperty(PROPERTY_PROXY_PASSWORD); String strDomainName = AppPropertiesService.getProperty(PROPERTY_DOMAIN_NAME); if (StringUtils.isNotBlank(strUserName) && StringUtils.isNotBlank(strPassword)) { // at least Userpasswordcredz Credentials creds; if (StringUtils.isBlank(strDomainName)) { creds = new UsernamePasswordCredentials(strUserName, strPassword); } else { creds = new NTCredentials(strUserName, strPassword, "", strDomainName); } CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, creds); client.setCredentialsProvider(credsProvider); HttpHost proxy = new HttpHost(AppPropertiesService.getProperty(PROPERTY_PROXY_HOST), AppPropertiesService.getPropertyInt(PROPERTY_PROXY_PORT, 8080)); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } return client; }
From source file:org.webservice.fotolia.FotoliaApi.java
/** * Construct and returns a usuable http client * * @param auto_refresh_token/*from w w w . java 2 s . co m*/ * @return DefaultHttpClient */ private DefaultHttpClient _getHttpClient(final boolean auto_refresh_token) { DefaultHttpClient client; client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(this._api_key, this._getSessionId(auto_refresh_token))); HttpConnectionParams.setConnectionTimeout(client.getParams(), FotoliaApi.API_CONNECT_TIMEOUT * 1000); HttpConnectionParams.setSoTimeout(client.getParams(), FotoliaApi.API_PROCESS_TIMEOUT * 1000); return client; }
From source file:com.nimbits.user.GoogleAuthentication.java
private Cookie getAuthCookie(final String gaeAppBaseUrl, final String gaeAppLoginUrl, final String authToken) throws NimbitsException { final DefaultHttpClient httpClient = new DefaultHttpClient(); Cookie retObj = null;//from w w w . ja va2s . c om final String cookieUrl; try { cookieUrl = gaeAppLoginUrl + "?continue=" + URLEncoder.encode(gaeAppBaseUrl, Const.CONST_ENCODING) + "&auth=" + URLEncoder.encode(authToken, Const.CONST_ENCODING); // // String cookieUrl = gaeAppLoginUrl + "?continue=" // + URLEncoder.encode(gaeAppBaseUrl,Const.CONST_ENCODING) + "&auth=" + authToken; //httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); final HttpGet httpget = new HttpGet(cookieUrl); final HttpResponse response = httpClient.execute(httpget); if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK || response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_NO_CONTENT) { for (final Cookie cookie : httpClient.getCookieStore().getCookies()) { if (cookie.getName().equals(Parameters.acsid.getText())) { retObj = cookie; break; } } } else if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) { throw new NimbitsException("invalid token"); } else { throw new NimbitsException( "Error getting cookie: status code:" + response.getStatusLine().getStatusCode()); } httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); } catch (UnsupportedEncodingException e) { throw new NimbitsException(e.getMessage()); } catch (ClientProtocolException e) { throw new NimbitsException(e.getMessage()); } catch (IOException e) { throw new NimbitsException(e.getMessage()); } return retObj; }
From source file:sand.actionhandler.weibo.UdaClient.java
public static AccessToken getToken(String userid, String password) throws Exception { //https://api.weibo.com/oauth2/authorize?client_id=2517196057&redirect_uri=http://116.236.101.196:18080/weibo.WeiBoAH.weibo&response_type=code&state=&scope= String url = "https://api.weibo.com/oauth2/authorize"; String content = ""; DefaultHttpClient httpclient = new DefaultHttpClient(); try {/*w w w. j ava2s. c om*/ httpclient = createHttpClient(); HttpPost httppost = new HttpPost(url); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("client_id", "2517196057")); nameValuePairs.add(new BasicNameValuePair("redirect_uri", REDIRECT_URI)); nameValuePairs.add(new BasicNameValuePair("userId", userid)); nameValuePairs.add(new BasicNameValuePair("passwd", password)); nameValuePairs.add(new BasicNameValuePair("isLoginSina", "")); nameValuePairs.add(new BasicNameValuePair("action", "submit")); nameValuePairs.add(new BasicNameValuePair("response_type", "code")); nameValuePairs.add(new BasicNameValuePair("withOfficalFlag", "0")); nameValuePairs.add(new BasicNameValuePair("ticket", "")); nameValuePairs.add(new BasicNameValuePair("'regCallback'", "")); nameValuePairs.add(new BasicNameValuePair("state", "")); nameValuePairs.add(new BasicNameValuePair("from", "")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // headers.add(new Header("Referer", "https://api.weibo.com/oauth2/authorize?client_id=your_client_id&redirect_uri=your_redirect_url&from=sina&response_type=code"));//referer // headers.add(new Header("Host", "api.weibo.com")); // headers.add(new Header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0")); httppost.addHeader("Referer", "https://api.weibo.com/oauth2/authorize?client_id=2517196057&redirect_uri=" + REDIRECT_URI + "&from=sina&response_type=code"); httppost.addHeader("Host", "api.weibo.com"); httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"); httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965); httppost.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); CookieStore cookieStore = new BasicCookieStore(); addCookie(cookieStore); // httpclient.setCookieStore(cookieStore); //httpclient.ex System.out.println(httppost.getURI()); System.out.println("https://api.weibo.com/oauth2/authorize?client_id=2517196057&redirect_uri=" + REDIRECT_URI + "&from=sina&response_type=code"); HttpResponse response = httpclient.execute(httppost); System.out.println("response " + response.getAllHeaders()); for (Header h : response.getAllHeaders()) { System.out.println(h.getName() + ", " + h.getValue()); } logger.info("response " + response); Header location = response.getFirstHeader("Location"); logger.info("location " + location); System.out.println("location " + location); if (location != null) { String retUrl = location.getValue(); System.out.println(retUrl); logger.info("retUrl " + retUrl); //String reU = location.getValue(); int begin = retUrl.indexOf("code="); if (begin != -1) { int end = retUrl.indexOf("&", begin); if (end == -1) end = retUrl.length(); String code = retUrl.substring(begin + 5, end); if (code != null) { Oauth oauth = new Oauth(); System.out.println("code is " + code); AccessToken token = oauth.getAccessTokenByCode(code); return token; } } } // HttpEntity entity = response.getEntity(); // // if (entity != null) { // content = EntityUtils.toString(entity); // System.out.println(content); // System.out.println("----------------------------------------"); // System.out.println("Uncompressed size: "+content.length()); // } } catch (Exception e) { // TODO Auto-generated catch block logger.error("error", e); e.printStackTrace(); throw e; } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } return null; }
From source file:cn.ctyun.amazonaws.http.HttpClientFactory.java
/** * Creates a new HttpClient object using the specified AWS * ClientConfiguration to configure the client. * * @param config/*from ww w. j a v a2 s.com*/ * Client configuration options (ex: proxy settings, connection * limits, etc). * * @return The new, configured HttpClient. */ public HttpClient createHttpClient(ClientConfiguration config) { /* Set HTTP client parameters */ HttpParams httpClientParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout()); HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, true); 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); httpClient.setRedirectStrategy(new LocationHeaderNotRequiredRedirectStrategy()); try { Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory()); SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getDefault(), SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); Scheme https = new Scheme("https", 443, sf); SchemeRegistry sr = connectionManager.getSchemeRegistry(); sr.register(http); sr.register(https); } catch (NoSuchAlgorithmException e) { throw new AmazonClientException("Unable to access default SSL context", e); } /* * If SSL cert checking for endpoints has been explicitly disabled, * register a new scheme for HTTPS that won't cause self-signed certs to * error out. */ if (System.getProperty("com.amazonaws.sdk.disableCertChecking") != null) { Scheme sch = new Scheme("https", 443, new TrustingSocketFactory()); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } /* Set proxy if configured */ String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { AmazonHttpClient.log .info("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:pt.lunacloud.http.HttpClientFactory.java
/** * Creates a new HttpClient object using the specified AWS * ClientConfiguration to configure the client. * * @param config//from w w w . jav a 2s . co m * 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(); 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); httpClient.setRedirectStrategy(new LocationHeaderNotRequiredRedirectStrategy()); try { Scheme http = new Scheme("http", 80, PlainSocketFactory.getSocketFactory()); SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getDefault(), SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); Scheme https = new Scheme("https", 443, sf); SchemeRegistry sr = connectionManager.getSchemeRegistry(); sr.register(http); sr.register(https); } catch (NoSuchAlgorithmException e) { throw new LunacloudClientException("Unable to access default SSL context"); } /* * If SSL cert checking for endpoints has been explicitly disabled, * register a new scheme for HTTPS that won't cause self-signed certs to * error out. */ if (System.getProperty("com.amazonaws.sdk.disableCertChecking") != null) { Scheme sch = new Scheme("https", 443, new TrustingSocketFactory()); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } /* Set proxy if configured */ String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { AmazonHttpClient.log .info("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; }