List of usage examples for org.apache.http.params HttpConnectionParams setSoTimeout
public static void setSoTimeout(HttpParams httpParams, int i)
From source file:com.nxt.zyl.data.volley.toolbox.HttpClientStack.java
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders); // add gzip support, not all request need gzip support if (request.isShouldGzip()) { httpRequest.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); }/*from w w w . j a v a2s. c o m*/ addHeaders(httpRequest, additionalHeaders); addHeaders(httpRequest, request.getHeaders()); onPrepareRequest(httpRequest); HttpParams httpParams = httpRequest.getParams(); int timeoutMs = request.getTimeoutMs(); // TODO: Reevaluate this connection timeout based on more wide-scale // data collection and possibly different for wifi vs. 3G. HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIME_OUT_MS); HttpConnectionParams.setSoTimeout(httpParams, timeoutMs); HttpResponse response = mClient.execute(httpRequest); if (response != null) { final HttpEntity entity = response.getEntity(); if (entity == null) { return response; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } return response; }
From source file:io.coldstart.android.API.java
public API() { HttpParams params = new BasicHttpParams(); this.client = new DefaultHttpClient(); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); this.mgr = new ThreadSafeClientConnManager(params, registry); this.httpclient = new DefaultHttpClient(mgr, client.getParams()); //Timeout ---------------------------------- HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 20000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 30000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); httpclient.setParams(httpParameters); //Timeout ---------------------------------- }
From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java
public static JsonNode postToServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;/*from ww w .ja va 2 s.c o m*/ LOG.d("POST api request, url = " + urlString + " object = " + objectToPost.toString()); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HttpPost httppost = null; URL url = null; try { url = new URL(urlString); httppost = new HttpPost(url.toString()); httppost.setHeader("Content-type", "application/json"); httppost.setHeader("Accept", ACCEPT); httppost.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);// , HTTP.UTF_8 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) { LOG.e(e.getLocalizedMessage()); } } return ret; }
From source file:com.zekke.services.http.RequestBuilder.java
public RequestBuilder setTimeout(int timeout) { HttpParams httpParams = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, timeout); HttpConnectionParams.setSoTimeout(httpParams, timeout); return this; }
From source file:org.casquesrouges.missing.HttpAdapter.java
@Override public void run() { handler.sendMessage(Message.obtain(handler, HttpAdapter.START)); httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, 80), new UsernamePasswordCredentials(login, pass)); HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000); try {//from w ww .j av a 2 s . c om HttpResponse response = null; switch (method) { case GET: response = httpClient.execute(new HttpGet(url)); break; case POST: HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(data)); response = httpClient.execute(httpPost); break; case FILE: File input = new File(picFile); HttpPost post = new HttpPost(url); MultipartEntity multi = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multi.addPart("person_id", new StringBody(Integer.toString(personID))); multi.addPart("creator_id", new StringBody(creator_id)); multi.addPart("file", new FileBody(input)); post.setEntity(multi); Log.d("MISSING", "http FILE: " + url + " pic: " + picFile); response = httpClient.execute(post); break; } processEntity(response); } catch (Exception e) { handler.sendMessage(Message.obtain(handler, HttpAdapter.ERROR, e)); } ConnectionManager.getInstance().didComplete(this); }
From source file:com.louding.frame.http.download.SimpleDownloader.java
/** * ?httpClient/* w ww . j a v a2 s.c o m*/ */ private void initHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, config.timeOut); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(config.maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, config.maxConnections); HttpConnectionParams.setSoTimeout(httpParams, config.timeOut); HttpConnectionParams.setConnectionTimeout(httpParams, config.timeOut); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, config.socketBuffer); 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); context = new SyncBasicHttpContext(new BasicHttpContext()); client = new DefaultHttpClient(cm, httpParams); client.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.httpHeader.entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } } }); client.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; } } } } }); client.setHttpRequestRetryHandler(new RetryHandler(config.timeOut)); }
From source file:org.safegees.safegees.util.HttpUrlConnection.java
public String performPostCall(String requestURL, HashMap<String, String> postDataParams, String userCredentials) { DefaultHttpClient httpclient = new DefaultHttpClient(); final HttpParams httpConnParams = httpclient.getParams(); HttpConnectionParams.setConnectionTimeout(httpConnParams, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(httpConnParams, READ_TIMEOUT); HttpPost httpPost = new HttpPost(requestURL); try {/*ww w. j a va 2 s . c o m*/ String postDataParamsString = getDataString(postDataParams); httpPost.setEntity(new StringEntity(postDataParamsString)); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); //Add the auth header if (userCredentials != null) httpPost.addHeader(KEY_HEADER_AUTHORIZED, userCredentials); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } HttpResponse response = null; String responseStr = null; try { response = httpclient.execute(httpPost); if (response != null) { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == 200 || statusLine.getStatusCode() == 201 || statusLine.getStatusCode() == 204) { //responseStr = EntityUtils.toString(response.getEntity(), HTTP.UTF_8); responseStr = statusLine.getReasonPhrase(); } else { Log.e("POST ERROR", response.getStatusLine().toString()); } } } catch (IOException e) { e.printStackTrace(); } return responseStr; }
From source file:com.volley.toolbox.HttpClientStack.java
/** * //from w ww .j av a2s .co m * @param request the request to perform * @param additionalHeaders additional headers to be sent together with * {@link Request#getHeaders()} * @return * @throws IOException * @throws AuthFailureError */ @Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { //request ??httprequest? httpurlrequest HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders); addHeaders(httpRequest, additionalHeaders); addHeaders(httpRequest, request.getHeaders()); onPrepareRequest(httpRequest); HttpParams httpParams = httpRequest.getParams(); int timeoutMs = request.getTimeoutMs(); // TODO: Reevaluate this connection timeout based on more wide-scale // data collection and possibly different for wifi vs. 3G. HttpConnectionParams.setConnectionTimeout(httpParams, 5000); HttpConnectionParams.setSoTimeout(httpParams, timeoutMs); return mClient.execute(httpRequest); }
From source file:fast.simple.download.http.DownloadHttpClient.java
/** * Create a new HttpClient with reasonable defaults (which you can update). * //w w w. j a v a2 s.c o m * @param userAgent * to report in your HTTP requests * @param context * to use for caching SSL sessions (may be null for no caching) * @return AndroidHttpClient for you to use for all your requests. */ public static DownloadHttpClient newInstance(String userAgent, Context context) { 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); HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT); HttpConnectionParams.setSocketBufferSize(params, 8192); // Don't handle redirects -- return them to the caller. Our code // often wants to re-POST after a redirect, which we must do ourselves. //HttpClientParams.setRedirecting(params, true); // Use a session cache for SSL sockets SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context); // Set the specified user agent and register standard protocols. HttpProtocolParams.setUserAgent(params, userAgent); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLCertificateSocketFactory.getHttpSocketFactory(SOCKET_OPERATION_TIMEOUT, sessionCache), 443)); ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); // We use a factory method to modify superclass initialization // parameters without the funny call-a-static-method dance. return new DownloadHttpClient(manager, params); }