private static HttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(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(); } }
public static HttpClient get() { HttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, 3000); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(30)); ConnManagerParams.setMaxTotalConnections(httpParams, 30); HttpClientParams.setRedirecting(httpParams, true); HttpProtocolParams.setUseExpectContinue(httpParams, true); HttpConnectionParams.setStaleCheckingEnabled(httpParams, false); HttpConnectionParams.setSoTimeout(httpParams, 2000); HttpConnectionParams.setConnectionTimeout(httpParams, 2000); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, 8192); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme(IDataSource.SCHEME_HTTP_TAG, PlainSocketFactory.getSocketFactory(), 80)); try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new MySSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); schemeRegistry.register(new Scheme(IDataSource.SCHEME_HTTPS_TAG, sf, 443)); } catch (Exception ex) { ex.printStackTrace(); } return new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams); }
public static b a(String str) { HttpParams basicHttpParams = new BasicHttpParams(); HttpProtocolParams.setVersion(basicHttpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(basicHttpParams, false); HttpConnectionParams.setStaleCheckingEnabled(basicHttpParams, false); HttpConnectionParams.setConnectionTimeout(basicHttpParams, 20000); HttpConnectionParams.setSoTimeout(basicHttpParams, 30000); HttpConnectionParams.setSocketBufferSize(basicHttpParams, 8192); HttpClientParams.setRedirecting(basicHttpParams, true); HttpClientParams.setAuthenticating(basicHttpParams, false); HttpProtocolParams.setUserAgent(basicHttpParams, str); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme(com.alipay.sdk.cons.b.a, SSLCertificateSocketFactory.getHttpSocketFactory(30000, null), WebSocket.DEFAULT_WSS_PORT)); ClientConnectionManager threadSafeClientConnManager = new ThreadSafeClientConnManager(basicHttpParams, schemeRegistry); ConnManagerParams.setTimeout(basicHttpParams, 60000); ConnManagerParams.setMaxConnectionsPerRoute(basicHttpParams, new ConnPerRouteBean(10)); ConnManagerParams.setMaxTotalConnections(basicHttpParams, 50); Security.setProperty("networkaddress.cache.ttl", "-1"); HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); return new b(threadSafeClientConnManager, basicHttpParams); }
public String getAsString(String url) throws Exception { HttpGet get = new HttpGet(url); HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects",false); get.setParams(params); HttpResponse r = httpClient.execute(get); return EntityUtils.toString(r.getEntity()); }
@Override public void run() { try { BasicHttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 500); HttpConnectionParams.setSoTimeout(httpParams, 500); HttpClient httpclient = new DefaultHttpClient(httpParams); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("latitude", "" + latitude)); params.add(new BasicNameValuePair("longitude", "" + longitude)); params.add(new BasicNameValuePair("userid", userId)); //服务器地址,指向Servlet HttpPost httpPost = new HttpPost(ServerUtil.SLUpdateLocation); final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");//以UTF-8格式发送 httpPost.setEntity(entity); //对提交数据进行编码 httpclient.execute(httpPost); } catch (Exception e) { } }
/** * 设置默认请求参数,并返回HttpClient * * @return HttpClient */ private HttpClient createHttpClient() { HttpParams mDefaultHttpParams = new BasicHttpParams(); //设置连接超时 HttpConnectionParams.setConnectionTimeout(mDefaultHttpParams, 15000); //设置请求超时 HttpConnectionParams.setSoTimeout(mDefaultHttpParams, 15000); HttpConnectionParams.setTcpNoDelay(mDefaultHttpParams, true); HttpProtocolParams.setVersion(mDefaultHttpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(mDefaultHttpParams, HTTP.UTF_8); //持续握手 HttpProtocolParams.setUseExpectContinue(mDefaultHttpParams, true); HttpClient mHttpClient = new DefaultHttpClient(mDefaultHttpParams); return mHttpClient; }
public DefaultHttpClient getHttpClientInstance() { if (this.httpClient != null) { return this.httpClient; } HttpParams params = new BasicHttpParams(); params.setParameter("http.connection.timeout", CONNECTION_TIMEOUT); params.setParameter("http.socket.timeout", SOCKET_TIMEOUT); params.setParameter("http.protocol.version", HttpVersion.HTTP_1_1); params.setParameter("http.useragent", "Apache-HttpClient/Android"); params.setParameter("http.connection.stalecheck", Boolean.valueOf(false)); this.httpClient = new DefaultHttpClient(params); this.httpClient.addRequestInterceptor(new GzipHttpRequestInterceptor()); this.httpClient.addResponseInterceptor(new GzipHttpResponseInterceptor()); this.httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return this.httpClient; }
public HttpResponse execute(HttpRequest request) throws IOException, HttpException { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProcessor processor = new ImmutableHttpProcessor(new RequestContent()); HttpRequestExecutor executor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); context.setAttribute(ExecutionContext.HTTP_CONNECTION, connection); if (!connection.isOpen()) { Socket socket = new Socket(address.getAddress(), address.getPort()); connection.bind(socket, params); } context.setAttribute(ExecutionContext.HTTP_REQUEST, request); request.setParams(params); executor.preProcess(request, processor, context); HttpResponse response = executor.execute(request, connection, context); executor.postProcess(response, processor, context); return response; }
private HttpParams createHttpParams() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUseExpectContinue(params, false); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(3)); ConnManagerParams.setMaxTotalConnections(params, 3); ConnManagerParams.setTimeout(params, 1000); HttpConnectionParams.setConnectionTimeout(params, 30000); HttpConnectionParams.setSoTimeout(params, 30000); HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpClientParams.setRedirecting(params, false); return params; }
public synchronized static DefaultHttpClient getHttpClient() { try { HttpParams params = new BasicHttpParams(); // 设置一些基本参数 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 超时设置 // 从连接池中取连接的超时时间 ConnManagerParams.setTimeout(params, 10000); // 连接超时 HttpConnectionParams.setConnectionTimeout(params, 10000); // 请求超时 HttpConnectionParams.setSoTimeout(params, 30000); SchemeRegistry registry = new SchemeRegistry(); Scheme sch1 = new Scheme("http", PlainSocketFactory .getSocketFactory(), 80); registry.register(sch1); // 使用线程安全的连接管理来创建HttpClient ClientConnectionManager conMgr = new ThreadSafeClientConnManager( params, registry); mHttpClient = new DefaultHttpClient(conMgr, params); } catch (Exception e) { e.printStackTrace(); } return mHttpClient; }
private d() { try { HandlerThread handlerThread = new HandlerThread("StatDispatcher"); handlerThread.start(); d = handlerThread.getId(); this.b = new Handler(handlerThread.getLooper()); HttpParams basicHttpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(basicHttpParams, 10000); HttpConnectionParams.setSoTimeout(basicHttpParams, 10000); this.a = new DefaultHttpClient(basicHttpParams); this.a.setKeepAliveStrategy(new e(this)); if (StatConfig.b() != null) { this.a.getParams().setParameter("http.route.default-proxy", StatConfig.b()); } } catch (Object th) { c.e(th); } }
/** * Returns the one <code>WebServiceUtil</code> instance * @return the one <code>WebServiceUtil</code> instance */ public static WebServiceUtil getInstance() { // This needs to be here instead of in the constructor because // it uses classes that are in the AndroidSDK and thus would // cause Stub! errors when running the component descriptor. synchronized(httpClientSynchronizer) { if (httpClient == null) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); BasicHttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); HttpConnectionParams.setSoTimeout(params, 20 * 1000); ConnManagerParams.setMaxTotalConnections(params, 20); ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); WebServiceUtil.httpClient = new DefaultHttpClient(manager, params); } } return INSTANCE; }
/** * Gets a DefaultHttpClient which trusts a set of certificates specified by the KeyStore * * @param keyStore custom provided KeyStore instance * @return DefaultHttpClient */ public static DefaultHttpClient getNewHttpClient(KeyStore keyStore) { try { SSLSocketFactory sf = new MySSLSocketFactory(keyStore); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }
public static SchemeRegistry getSchemeRegistry() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier()); SSLSocketFactory sf = new SSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); 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)); return registry; } catch (Exception e) { return null; } }
/** * Gets getUrl DefaultHttpClient which trusts getUrl set of certificates specified by the KeyStore * * @param keyStore custom provided KeyStore instance * @return DefaultHttpClient */ public static DefaultHttpClient getNewHttpClient(KeyStore keyStore) { try { SSLSocketFactory sf = new MySSLSocketFactory(keyStore); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }
public RequestListenerThread(Context context) throws IOException, BindException { serversocket = new ServerSocket(PORT); params = new BasicHttpParams(); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000).setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true).setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1"); // Set up the HTTP protocol processor BasicHttpProcessor httpProcessor = new BasicHttpProcessor(); httpProcessor.addInterceptor(new ResponseDate()); httpProcessor.addInterceptor(new ResponseServer()); httpProcessor.addInterceptor(new ResponseContent()); httpProcessor.addInterceptor(new ResponseConnControl()); // Set up the HTTP service this.httpService = new YaaccHttpService(httpProcessor, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory(), context); }
static void init(final NetLayer lowerNetLayer) { try { HttpClientUtil.lowerNetLayer = lowerNetLayer; final Scheme http = new Scheme("http", new NetlibSocketFactory(lowerNetLayer), 80); supportedSchemes = new SchemeRegistry(); supportedSchemes.register(http); // prepare parameters final HttpParams httpParams = new BasicHttpParams(); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParams, Util.UTF8); HttpProtocolParams.setUseExpectContinue(httpParams, true); // connMgr = new ThreadSafeClientConnManager(httpParams, supportedSchemes); } catch (final Exception e) { LOG.error("error during class init", e); } }
public static HttpPost getHttpPost(String uri, Map<String, String> parameters) throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(uri); httpPost.addHeader("User-Agent", "HTTP Client"); httpPost.addHeader("Content-type", "application/x-www-form-urlencoded"); //postMethod.addHeader("Content-Length", "0"); HttpParams params = new BasicHttpParams(); httpPost.setParams(params); List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); Set<String> keys = parameters.keySet(); for (String key : keys) { String value = parameters.get(key); postParameters.add(new BasicNameValuePair(key, value)); } Debug.logInfo("SagePay PostParameters - " + postParameters, module); HttpEntity postEntity = new UrlEncodedFormEntity(postParameters); httpPost.setEntity(postEntity); return httpPost; }
private DefaultHttpClient createHttpClient() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); HttpProtocolParams.setUseExpectContinue(params, true); HttpConnectionParams.setConnectionTimeout(params, CONNETED_TIMEOUT * 1000); HttpConnectionParams.setSoTimeout(params, CONNETED_TIMEOUT * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); ConnManagerParams.setMaxTotalConnections(params, 4); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager connMgr = new ThreadSafeClientConnManager( params, schReg); return new DefaultHttpClient(connMgr, params); }
private static HttpParams createHttpParams() { final HttpParams params = new BasicHttpParams(); // 设置是否启用旧连接检查,默认是开启的。关闭这个旧连接检查可以提高一点点性能,但是增加了I/O错误的风险(当服务端关闭连接时)。 // 开启这个选项则在每次使用老的连接之前都会检查连接是否可用,这个耗时大概在15-30ms之间 HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// 设置链接超时时间 HttpConnectionParams.setSoTimeout(params, TIMEOUT);// 设置socket超时时间 HttpConnectionParams.setSocketBufferSize(params, SOCKET_BUFFER_SIZE);// 设置缓存大小 HttpConnectionParams.setTcpNoDelay(params, true);// 是否不使用延迟发送(true为不延迟) HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 设置协议版本 HttpProtocolParams.setUseExpectContinue(params, true);// 设置异常处理机制 HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);// 设置编码 HttpClientParams.setRedirecting(params, false);// 设置是否采用重定向 ConnManagerParams.setTimeout(params, TIMEOUT);// 设置超时 ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(MAX_CONNECTIONS));// 多线程最大连接数 ConnManagerParams.setMaxTotalConnections(params, 10); // 多线程总连接数 return params; }
/** * Performs general setup. * This should be called only once. */ private final static void setup() { // Register the "http" and "https" protocol schemes, they are // required by the default operator to look up socket factories. supportedSchemes = new SchemeRegistry(); SocketFactory sf = PlainSocketFactory.getSocketFactory(); supportedSchemes.register(new Scheme("http", sf, 80)); sf = SSLSocketFactory.getSocketFactory(); supportedSchemes.register(new Scheme("https", sf, 80)); // Prepare parameters. // Since this example doesn't use the full core framework, // only few parameters are actually required. HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(params, false); defaultParameters = params; }
/** * Performs general setup. * This should be called only once. */ private final static void setup() { // Register the "http" protocol scheme, it is required // by the default operator to look up socket factories. supportedSchemes = new SchemeRegistry(); SocketFactory sf = PlainSocketFactory.getSocketFactory(); supportedSchemes.register(new Scheme("http", sf, 80)); // Prepare parameters. // Since this example doesn't use the full core framework, // only few parameters are actually required. HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(params, false); defaultParameters = params; }
/** * 此处解释下MaxtTotal和DefaultMaxPerRoute的区别: * 1、MaxtTotal是整个池子的大小; * 2、DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分;比如: * MaxtTotal=400 DefaultMaxPerRoute=200 * 而我只连接到http://sishuok.com时,到这个主机的并发最多只有200;而不是400; * 而我连接到http://sishuok.com 和 http://qq.com时,到每个主机的并发最多只有200;即加起来是400(但不能超过400);所以起作用的设置是DefaultMaxPerRoute。 */ public HttpParams getHttpParams() { HttpParams params = new BasicHttpParams(); // 设置连接超时时间 Integer CONNECTION_TIMEOUT = 2 * 1000; // 设置请求超时2秒钟 根据业务调整 Integer SO_TIMEOUT = 2 * 1000; // 设置等待数据超时时间2秒钟 根据业务调整 // 定义了当从ClientConnectionManager中检索ManagedClientConnection实例时使用的毫秒级的超时时间 // 这个参数期望得到一个java.lang.Long类型的值。如果这个参数没有被设置,默认等于CONNECTION_TIMEOUT,因此一定要设置 Long CONN_MANAGER_TIMEOUT = 500L; // 该值就是连接不够用的时候等待超时时间,一定要设置,而且不能太大 () params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT); params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT); // 在提交请求之前 测试连接是否可用 params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true); return params; }
/** * 微信自定义信任管理器X509TrustManager * @param httpclient * @return */ private static HttpClient initHttpClient(HttpClient httpclient) { try { TrustManager[] tm = { new MyX509TrustManager() }; // 取得SSL的SSLContext实例 SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); // 初始化SSLContext sslContext.init(null, tm, new java.security.SecureRandom()); // 从上述SSLContext对象中得到SSLSocketFactory对象 SocketFactory ssf = (SocketFactory) sslContext.getSocketFactory(); ClientConnectionManager ccm = new DefaultHttpClient().getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", ssf, 8443)); HttpParams params = new BasicHttpParams(); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); httpclient = new DefaultHttpClient(ccm, params); } catch (Exception ex) { Exceptions.printException(ex); } return httpclient; }
/** * 每次返回一个新的HttpClient实例 * @author nan.li * @return */ public static DefaultHttpClient getHttpClient() { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(500); cm.setDefaultMaxPerRoute(200); HttpParams params = new BasicHttpParams(); params.setParameter("http.connection.timeout", Integer.valueOf(CON_TIMEOUT)); params.setParameter("http.socket.timeout", Integer.valueOf(SO_TIMEOUT)); params.setParameter("http.useragent", UA_WINDOW7_CHROME); DefaultHttpClient client = new DefaultHttpClient(cm, params); return client; }
/** * Construct a JsonRPCClient with the given service uri * * @param uri * uri of the service */ public JSONRPCHttpClient(String uri) { SSLSocketFactory allowAllsslFactory = null; try { allowAllsslFactory = new AllowAllSSLSocketFactory(null); } catch (Exception e) { e.printStackTrace(); } allowAllsslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // Quick ugly fix to accept ssl certificates HttpParams params = new BasicHttpParams(); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", allowAllsslFactory, 443)); ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(params, registry); httpClient = new DefaultHttpClient(ccm, params); serviceUri = uri; }
/** * Create a thread-safe client. This client does not do redirecting, to allow us to capture * correct "error" codes. * * @return HttpClient */ public static final DefaultHttpClient createHttpClient() { // Shamelessly cribbed from AndroidHttpClient HttpParams params = new BasicHttpParams(); // Turn off stale checking. Our connections break all the time anyway, // and it's not worth it to pay the penalty of checking every time. HttpConnectionParams.setStaleCheckingEnabled(params, false); // Default connection and socket timeout of 10 seconds. Tweak to taste. HttpConnectionParams.setConnectionTimeout(params, 10 * 1000); HttpConnectionParams.setSoTimeout(params, 10 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); // Sets up the http part of the service. final SchemeRegistry supportedSchemes = new SchemeRegistry(); // Register the "http" protocol scheme, it is required // by the default operator to look up socket factories. final SocketFactory sf = PlainSocketFactory.getSocketFactory(); supportedSchemes.register(new Scheme("http", sf, 80)); final ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, supportedSchemes); return new DefaultHttpClient(ccm, params); }
/** generates a HTTP-Client that also supports HTTPS **/ private static DefaultHttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); BasicSSLSocketFactory sf = new BasicSSLSocketFactory(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(); // setting connections types 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(); } }
/** * Do a http get for the given url. * * @param url requested url * @param parameters request parameters * @param accessor oauth accessor * @return the http response * @throws IOException * @throws {@link YNoteException} */ public static HttpResponse doGet(String url, Map<String, String> parameters, OAuthAccessor accessor) throws IOException, YNoteException { // add ynote parameters to the url OAuth.addParameters(url, parameters == null ? null : parameters.entrySet()); HttpGet get = new HttpGet(url); // sign all parameters, including oauth parameters and ynote parameters // and add the oauth related information into the header Header oauthHeader = getAuthorizationHeader(url, OAuthMessage.GET, parameters, accessor); get.addHeader(oauthHeader); HttpParams params = new BasicHttpParams(); HttpClientParams.setRedirecting(params, false); get.setParams(params); HttpResponse response = client.execute(get); if ((response.getStatusLine().getStatusCode() / 100) != 2) { YNoteException e = wrapYNoteException(response); throw e; } return response; }
private static DefaultHttpClient getClient() { if (client == null || !REUSE_CLIENT) { AQUtility.debug("creating http client"); HttpParams httpParams = new BasicHttpParams(); //httpParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpConnectionParams.setConnectionTimeout(httpParams, NET_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParams, NET_TIMEOUT); //ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(NETWORK_POOL)); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(25)); //Added this line to avoid issue at: http://stackoverflow.com/questions/5358014/android-httpclient-oom-on-4g-lte-htc-thunderbolt HttpConnectionParams.setSocketBufferSize(httpParams, 8192); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", ssf == null ? SSLSocketFactory.getSocketFactory() : ssf, 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, registry); client = new DefaultHttpClient(cm, httpParams); } return client; }
/** * 从可用的HttpClient池中返回一个默认10秒的HttpClient对象,该方法是同步的。 * * @return 可用的.HttpClient对象 */ public static synchronized HttpClient getHttpClient() { if (null == customerHttpClient) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, CHARSET); HttpProtocolParams.setUseExpectContinue(params, true); HttpProtocolParams.setUserAgent(params, "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) " + "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1"); ConnManagerParams.setTimeout(params, 10000); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); customerHttpClient = new DefaultHttpClient(conMgr, params); } return customerHttpClient; }
/** * 发送一个GET请求 * * @return * @throws Exception */ private HttpRequest get() throws Exception { HttpGet httpget = null; String result = null; int respCode = -1; try { httpget = new HttpGet(reqUrl); httpget.setHeader("Connection", "close"); HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 1000 * 60); httpget.setParams(params); HttpResponse response = new DefaultHttpClient().execute(httpget); result = EntityUtils.toString(response.getEntity(), charset).trim(); respCode = response.getStatusLine().getStatusCode(); if (respCode == HttpStatus.SC_OK) { this.respResult = result; } } catch (Exception e) { throw e; } finally { if (null != httpget) { httpget = null; } } return this; }
private static DefaultHttpClient getHttpClient() { // new DefaultHttpClient() // use following code to solve Adapter is detached error // refer: // http://stackoverflow.com/questions/5317882/android-handling-back-button-during-asynctask BasicHttpParams params = new BasicHttpParams(); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory .getSocketFactory(), 443)); // Set the timeout in milliseconds until a connection is established. // HttpConnectionParams.setConnectionTimeout(params, // CONNECTION_TIMEOUT); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. // HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); return new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params); }