@Override public DefaultHttpClient create(final HttpMethod method, final URI uri) { final TrustStrategy acceptTrustStrategy = new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] certificate, final String authType) { return true; } }; final SchemeRegistry registry = new SchemeRegistry(); try { final SSLSocketFactory ssf = new SSLSocketFactory(acceptTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); registry.register(new Scheme(uri.getScheme(), uri.getPort(), ssf)); } catch (Exception e) { throw new ODataRuntimeException(e); } final DefaultHttpClient httpClient = new DefaultHttpClient(new BasicClientConnectionManager(registry)); httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT); return httpClient; }
/** * * @param isMultiRequest * 是否支持多线程 * @param connectionTimeout * 建立连接超时时间(毫秒) * @param socketTimeout * 等待数据超时时间(毫秒) * @return */ public static HttpClient getHttpClient(boolean isMultiRequest, int connectionTimeout, int socketTimeout) { SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault(); ClientConnectionManager cm = isMultiRequest ? new PoolingClientConnectionManager( schemeRegistry) : new BasicClientConnectionManager( schemeRegistry); HttpParams params = isMultiRequest ? new SyncBasicHttpParams() : new BasicHttpParams(); HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setSoTimeout(params, socketTimeout); return new DefaultHttpClient(cm, params); }
@Override public CloseableHttpClient newInstance() { try { final SSLSocketFactory socketFactory = new SSLSocketFactory(this.getSSLContext()); socketFactory.setHostnameVerifier(new UnsafeHostNameVerifier()); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", 443, socketFactory)); final ClientConnectionManager clientConnectionManager = new BasicClientConnectionManager(schemeRegistry); return new DefaultHttpClient(clientConnectionManager); } catch (final KeyManagementException | NoSuchAlgorithmException ex) { throw new RuntimeException("Unable to instantiate HTTP Client", ex); } }
/** * Parses a web accessible WADL file * @param weburl * @param username * @param password * @param ignoreSSLErrors if true, SSL errors are ignored * @return a non-null "Application" object, represeting a WADL's application root XML * Sample code:<br> * <pre> * Application app = WADL2UDDI.parseWadl(new URL("http://server/wsdl.wsdl"), "username", "password", * clerkManager.getClientConfig().isX_To_Wsdl_Ignore_SSL_Errors() ); * </pre> */ public static Application parseWadl(URL weburl, String username, String password, boolean ignoreSSLErrors){ DefaultHttpClient httpclient = null; Application unmarshal=null; try { String url = weburl.toString(); if (!url.toLowerCase().startsWith("http")) { return parseWadl(weburl); } boolean usessl = false; int port = 80; if (url.toLowerCase().startsWith("https://")) { port = 443; usessl = true; } if (weburl.getPort() > 0) { port = weburl.getPort(); } if (ignoreSSLErrors && usessl) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", port, new MockSSLSocketFactory())); ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry); httpclient = new DefaultHttpClient(cm); } else { httpclient = new DefaultHttpClient(); } if (username != null && username.length() > 0 && password != null && password.length() > 0) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(weburl.getHost(), port), new UsernamePasswordCredentials(username, password)); } HttpGet httpGet = new HttpGet(url); try { HttpResponse response1 = httpclient.execute(httpGet); //System.out.println(response1.getStatusLine()); // HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed ResponseHandler<String> responseHandler = new BasicResponseHandler(); String handleResponse = responseHandler.handleResponse(response1); StringReader sr = new StringReader(handleResponse); unmarshal = (Application) XmlUtils.unmarshal(sr, Application.class); sr.close(); } finally { httpGet.releaseConnection(); } } catch (Exception e) { log.error(e.getMessage(), e); } finally { if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } return unmarshal; }
private InputSource getImportFromUrl(String url) { InputSource inputSource = null; DefaultHttpClient httpclient = null; try { URL url2 = new URL(url); if (!url.toLowerCase().startsWith("http")) { return getImportFromFile(url); } boolean usessl = false; int port = 80; if (url.toLowerCase().startsWith("https://")) { port = 443; usessl = true; } if (url2.getPort() > 0) { port = url2.getPort(); } if (ignoreSSLErrors && usessl) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", port, new MockSSLSocketFactory())); ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry); httpclient = new DefaultHttpClient(cm); } else { httpclient = new DefaultHttpClient(); } if (username != null && username.length() > 0 && password != null && password.length() > 0) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(url2.getHost(), port), new UsernamePasswordCredentials(username, password)); } HttpGet httpGet = new HttpGet(url); try { HttpResponse response1 = httpclient.execute(httpGet); //System.out.println(response1.getStatusLine()); // HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed ResponseHandler<String> responseHandler = new BasicResponseHandler(); String handleResponse = responseHandler.handleResponse(response1); StringReader sr = new StringReader(handleResponse); inputSource = new InputSource(sr); } finally { httpGet.releaseConnection(); } // InputStream inputStream = importUrl.openStream(); //inputSource = new InputSource(inputStream); latestImportURI = url; } catch (Exception e) { log.error(e.getMessage()); log.debug(e.getMessage(), e); lastException = e; } finally { if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } return inputSource; }