List of usage examples for org.apache.http.params HttpProtocolParams setVersion
public static void setVersion(HttpParams httpParams, ProtocolVersion protocolVersion)
From source file:de.uni_koblenz_landau.apow.helper.SyncHelper.java
/** * Creates a HTTPClient for usage with self signed SSL certificates. * /*from ww w .ja v a2s . c om*/ * Sources: * http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https * http://havrl.blogspot.de/2013/08/synchronization-algorithm-for.html * * @return HTTPClient */ private static HttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SelfSignedSSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }
From source file:fr.eolya.utils.http.HttpLoader.java
private HttpParams getHttpParams() { HttpParams httpParams = new BasicHttpParams(); // connection HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeOut); HttpConnectionParams.setSoTimeout(httpParams, sockeTimeOut); // protocol/*from ww w. j a v a 2 s . co m*/ HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParams, "utf-8"); // user-agent if (!StringUtils.isEmpty(userAgent)) { httpParams.setParameter(CoreProtocolPNames.USER_AGENT, userAgent); } // redirect httpParams.setParameter("http.protocol.handle-redirects", followRedirect); return httpParams; }
From source file:org.openmeetings.app.sip.xmlrpc.OpenXGHttpClient.java
public HttpClient getHttpClient() { try {// ww w. j a v a 2 s .c o m SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getInstance("TLS"), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); registry.register(new Scheme("https", 443, sf)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }
From source file:com.github.vseguip.sweet.rest.SugarRestAPI.java
/** * Get an HttpConnection object. Will create a new one if needed. * //from w w w.j ava 2 s .c om * @return A defualt HTTP connection object */ private HttpClient getConnection() { if (mHttpClient == null) { // registers schemes for both http and https HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUseExpectContinue(params, false); HttpConnectionParams.setConnectionTimeout(params, TIMEOUT_OPS); HttpConnectionParams.setSoTimeout(params, TIMEOUT_OPS); ConnManagerParams.setTimeout(params, TIMEOUT_OPS); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); if (!mNoCertValidation) registry.register(new Scheme("https", sslSocketFactory, 443)); else registry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry); mHttpClient = new DefaultHttpClient(manager, params); } return mHttpClient; }
From source file:org.dasein.cloud.rackspace.AbstractMethod.java
private @Nonnull HttpClient getClient() throws CloudException { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new CloudException("No context was provided for this request"); }//from ww w .j a v a2 s . c o m String endpoint = ctx.getEndpoint(); boolean ssl = (endpoint != null && endpoint.startsWith("https")); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); //noinspection deprecation HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUserAgent(params, "Dasein Cloud"); Properties p = ctx.getCustomProperties(); if (p != null) { String proxyHost = p.getProperty("proxyHost"); String proxyPort = p.getProperty("proxyPort"); if (proxyHost != null) { int port = 0; if (proxyPort != null && proxyPort.length() > 0) { port = Integer.parseInt(proxyPort); } params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, port, ssl ? "https" : "http")); } } return new DefaultHttpClient(params); }
From source file:org.dasein.cloud.zimory.ZimoryMethod.java
private @Nonnull HttpClient getClient(URI uri) throws InternalException, CloudException { ProviderContext ctx = provider.getContext(); if (ctx == null) { throw new NoContextException(); }//from w w w . j a va 2s . c o m boolean ssl = uri.getScheme().startsWith("https"); int targetPort = uri.getPort(); if (targetPort < 1) { targetPort = (ssl ? 443 : 80); } HttpParams params = new BasicHttpParams(); SchemeRegistry registry = new SchemeRegistry(); try { registry.register( new Scheme(ssl ? "https" : "http", targetPort, new X509SSLSocketFactory(new X509Store(ctx)))); } catch (KeyManagementException e) { e.printStackTrace(); throw new InternalException(e); } catch (UnrecoverableKeyException e) { e.printStackTrace(); throw new InternalException(e); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new InternalException(e); } catch (KeyStoreException e) { e.printStackTrace(); throw new InternalException(e); } HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); //noinspection deprecation HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUserAgent(params, ""); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 300000); Properties p = ctx.getCustomProperties(); if (p != null) { String proxyHost = p.getProperty("proxyHost"); String proxyPort = p.getProperty("proxyPort"); if (proxyHost != null) { int port = 0; if (proxyPort != null && proxyPort.length() > 0) { port = Integer.parseInt(proxyPort); } params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, port, ssl ? "https" : "http")); } } ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry); return new DefaultHttpClient(ccm, params); }
From source file:org.jasig.portal.security.provider.saml.SAMLDelegatedAuthenticationService.java
/** * This method takes the SOAP AuthnRequest, sends it to the IdP, and retrieves * the result. This method does not process the result. * /*w ww. j av a2 s . co m*/ * @param samlSession SAML session * @param authnState * @return true, if successful */ private boolean getSOAPResponse(SAMLSession samlSession, DelegatedSAMLAuthenticationState authnState) { this.logger.debug("Step 4 of 5: Get SOAP response from IDP"); String result = null; HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); params.setParameter("SOAPAction", "urn:liberty:ssos:2006-08:AuthnRequest"); HttpClient client = new DefaultHttpClient(params); try { logger.debug("Getting SOAP response from {} with POST body:\n{}", authnState.getIdpEndpoint(), authnState.getModifiedSOAPRequest()); setupIdPClientConnection(client, samlSession, authnState); HttpPost method = new HttpPost(authnState.getIdpEndpoint()); method.setHeader("Content-Type", "text/xml"); StringEntity postData = new StringEntity(authnState.getModifiedSOAPRequest(), HTTP.UTF_8); method.setEntity(postData); HttpResponse httpResponse = client.execute(method); int resultCode = httpResponse.getStatusLine().getStatusCode(); if (resultCode >= HttpStatus.SC_OK && resultCode < 300) { HttpEntity httpEntity = httpResponse.getEntity(); ByteArrayOutputStream output = new ByteArrayOutputStream(); httpEntity.writeTo(output); result = output.toString(); logger.debug("Got SOAP response:\n{}", result); authnState.setSoapResponse(result); return true; } logger.error("Unsupported HTTP result code when retrieving the resource: " + resultCode + "."); throw new DelegatedAuthenticationRuntimeException( "Unsupported HTTP result code when retrieving the resource: " + resultCode + "."); } catch (Exception ex) { logger.error("Exception caught when trying to retrieve the resource.", ex); throw new DelegatedAuthenticationRuntimeException( "Exception caught when trying to retrieve the resource.", ex); } finally { client.getConnectionManager().shutdown(); } }
From source file:com.lgallardo.youtorrentcontroller.JSONParser.java
public String getNewCookie() throws JSONParserStatusCodeException { String url = "login"; // if server is publish in a subfolder, fix url if (subfolder != null && !subfolder.equals("")) { url = subfolder + "/" + url; }/*from ww w.java 2s .co m*/ String cookieString = null; HttpResponse httpResponse; DefaultHttpClient httpclient; HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = connection_timeout * 1000; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = data_timeout * 1000; // Set http parameters HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUserAgent(httpParameters, "qBittorrent for Android"); HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); // Making HTTP request HttpHost targetHost = new HttpHost(hostname, port, protocol); // httpclient = new DefaultHttpClient(); httpclient = getNewHttpClient(); // Set http parameters httpclient.setParams(httpParameters); try { // AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); // // UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password); // // httpclient.getCredentialsProvider().setCredentials(authScope, credentials); url = protocol + "://" + hostname + ":" + port + "/" + url; HttpPost httpget = new HttpPost(url); // // In order to pass the username and password we must set the pair name value List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", this.username)); nvps.add(new BasicNameValuePair("password", this.password)); httpget.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = httpclient.execute(targetHost, httpget); HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); int mStatusCode = statusLine.getStatusCode(); if (mStatusCode == 200) { // Save cookie List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (!cookies.isEmpty()) { cookieString = cookies.get(0).getName() + "=" + cookies.get(0).getValue() + "; domain=" + cookies.get(0).getDomain(); cookieString = cookies.get(0).getName() + "=" + cookies.get(0).getValue(); } } if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } catch (Exception e) { Log.e("Debug", "Exception " + e.toString()); } if (cookieString == null) { cookieString = ""; } return cookieString; }
From source file:com.mike.aframe.http.KJHttp.java
/** * ?httpClient//from w w w. ja v a 2 s .c o m */ private void initHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, config.getConnectTimeOut()); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(config.getMaxConnections())); ConnManagerParams.setMaxTotalConnections(httpParams, config.getMaxConnections()); HttpConnectionParams.setSoTimeout(httpParams, config.getConnectTimeOut()); HttpConnectionParams.setConnectionTimeout(httpParams, config.getConnectTimeOut()); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, config.getSocketBuffer()); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams, "KJLibrary"); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } for (Entry<String, String> entry : config.getHeader().entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase("gzip")) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); threadPool = (ThreadPoolExecutor) KJThreadExecutors.newCachedThreadPool(); httpClient.setHttpRequestRetryHandler(new RetryHandler(config.getReadTimeout())); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); }
From source file:com.ridgelineapps.wallpaper.photosite.FlickrUtils.java
private FlickrUtils() { final HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); final SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry); mClient = new DefaultHttpClient(manager, params); }