List of usage examples for org.apache.http.params HttpParams setParameter
HttpParams setParameter(String str, Object obj);
From source file:nz.net.catalyst.MaharaDroid.upload.http.RestClient.java
public static JSONObject CallFunction(String url, String[] paramNames, String[] paramVals, Context context) { JSONObject json = new JSONObject(); SchemeRegistry supportedSchemes = new SchemeRegistry(); SSLSocketFactory sf = getSocketFactory(DEBUG); // TODO we make assumptions about ports. supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); supportedSchemes.register(new Scheme("https", sf, 443)); HttpParams http_params = new BasicHttpParams(); ClientConnectionManager ccm = new ThreadSafeClientConnManager(http_params, supportedSchemes); // HttpParams http_params = httpclient.getParams(); http_params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpConnectionParams.setConnectionTimeout(http_params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(http_params, CONNECTION_TIMEOUT); DefaultHttpClient httpclient = new DefaultHttpClient(ccm, http_params); if (paramNames == null) { paramNames = new String[0]; }/* ww w .jav a2s . c o m*/ if (paramVals == null) { paramVals = new String[0]; } if (paramNames.length != paramVals.length) { Log.w(TAG, "Incompatible number of param names and values, bailing on upload!"); return null; } SortedMap<String, String> sig_params = new TreeMap<String, String>(); HttpResponse response = null; HttpPost httppost = null; Log.d(TAG, "HTTP POST URL: " + url); try { httppost = new HttpPost(url); } catch (IllegalArgumentException e) { try { json.put("fail", e.getMessage()); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return json; } try { File file = null; // If this is a POST call, then it is a file upload. Check to see if // a // filename is given, and if so, open that file. // Get the title of the photo being uploaded so we can pass it into // the // MultipartEntityMonitored class to be broadcast for progress // updates. String title = ""; for (int i = 0; i < paramNames.length; ++i) { if (paramNames[i].equals("title")) { title = paramVals[i]; } else if (paramNames[i].equals("filename")) { file = new File(paramVals[i]); continue; } sig_params.put(paramNames[i], paramVals[i]); } MultipartEntityMonitored mp_entity = new MultipartEntityMonitored(context, title); if (file != null) { mp_entity.addPart("userfile", new FileBody(file)); } for (Map.Entry<String, String> entry : sig_params.entrySet()) { mp_entity.addPart(entry.getKey(), new StringBody(entry.getValue())); } httppost.setEntity(mp_entity); response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { String content = convertStreamToString(resEntity.getContent()); if (response.getStatusLine().getStatusCode() == 200) { try { json = new JSONObject(content.toString()); } catch (JSONException e1) { Log.w(TAG, "Response 200 received but invalid JSON."); json.put("fail", e1.getMessage()); if (DEBUG) Log.d(TAG, "HTTP POST returned status code: " + response.getStatusLine()); } } else { Log.w(TAG, "File upload failed with response code:" + response.getStatusLine().getStatusCode()); json.put("fail", response.getStatusLine().getReasonPhrase()); if (DEBUG) Log.d(TAG, "HTTP POST returned status code: " + response.getStatusLine()); } } else { Log.w(TAG, "Response does not contain a valid HTTP entity."); if (DEBUG) Log.d(TAG, "HTTP POST returned status code: " + response.getStatusLine()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { try { json.put("fail", e.getMessage()); } catch (JSONException e1) { } e.printStackTrace(); } catch (IllegalStateException e) { try { json.put("fail", e.getMessage()); } catch (JSONException e1) { } e.printStackTrace(); } catch (IllegalArgumentException e) { try { json.put("fail", e.getMessage()); } catch (JSONException e1) { } e.printStackTrace(); } catch (JSONException e) { } httpclient.getConnectionManager().shutdown(); return json; }
From source file:eu.thecoder4.gpl.pleftdroid.PleftBroker.java
/** * @return the client/*from ww w . j av a 2s . c o m*/ */ protected static DefaultHttpClient getDefaultClient() { // Init HTTP params HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, CONN_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParameters, SO_TIMEOUT); httpParameters.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); httpParameters.setParameter("http.protocol.content-charset", "UTF-8"); DefaultHttpClient client = new DefaultHttpClient(httpParameters); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "PleftDroid/1.0 (Linux; U; Android " + Build.VERSION.RELEASE + "/" + Build.VERSION.CODENAME + "; " + Locale.getDefault().getLanguage() + "; " + Build.MODEL + ")"); return client; }
From source file:org.jets3t.service.utils.RestUtils.java
public static HttpClient initHttpsConnection(final JetS3tRequestAuthorizer requestAuthorizer, Jets3tProperties jets3tProperties, String userAgentDescription, CredentialsProvider credentialsProvider) { // Configure HttpClient properties based on Jets3t Properties. HttpParams params = createDefaultHttpParams(); params.setParameter(Jets3tProperties.JETS3T_PROPERTIES_ID, jets3tProperties); params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, jets3tProperties.getStringProperty( ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, ConnManagerFactory.class.getName())); HttpConnectionParams.setConnectionTimeout(params, jets3tProperties.getIntProperty("httpclient.connection-timeout-ms", 60000)); HttpConnectionParams.setSoTimeout(params, jets3tProperties.getIntProperty("httpclient.socket-timeout-ms", 60000)); HttpConnectionParams.setStaleCheckingEnabled(params, jets3tProperties.getBoolProperty("httpclient.stale-checking-enabled", true)); // Connection properties to take advantage of S3 window scaling. if (jets3tProperties.containsKey("httpclient.socket-receive-buffer")) { HttpConnectionParams.setSocketBufferSize(params, jets3tProperties.getIntProperty("httpclient.socket-receive-buffer", 0)); }/* w w w .j a v a 2 s. c o m*/ HttpConnectionParams.setTcpNoDelay(params, true); // Set user agent string. String userAgent = jets3tProperties.getStringProperty("httpclient.useragent", null); if (userAgent == null) { userAgent = ServiceUtils.getUserAgentDescription(userAgentDescription); } if (log.isDebugEnabled()) { log.debug("Setting user agent string: " + userAgent); } HttpProtocolParams.setUserAgent(params, userAgent); boolean expectContinue = jets3tProperties.getBoolProperty("http.protocol.expect-continue", true); HttpProtocolParams.setUseExpectContinue(params, expectContinue); long connectionManagerTimeout = jets3tProperties.getLongProperty("httpclient.connection-manager-timeout", 0); ConnManagerParams.setTimeout(params, connectionManagerTimeout); DefaultHttpClient httpClient = wrapClient(params); httpClient.setHttpRequestRetryHandler(new JetS3tRetryHandler( jets3tProperties.getIntProperty("httpclient.retry-max", 5), requestAuthorizer)); if (credentialsProvider != null) { if (log.isDebugEnabled()) { log.debug("Using credentials provider class: " + credentialsProvider.getClass().getName()); } httpClient.setCredentialsProvider(credentialsProvider); if (jets3tProperties.getBoolProperty("httpclient.authentication-preemptive", false)) { // Add as the very first interceptor in the protocol chain httpClient.addRequestInterceptor(new PreemptiveInterceptor(), 0); } } return httpClient; }
From source file:com.base.httpclient.HttpJsonClient.java
/** * httpClient/*from ww w. j a va 2 s .c o m*/ * @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); HttpParams httpParams = new BasicHttpParams(); cm.setMaxTotal(10);// cm.setDefaultMaxPerRoute(5);// ? HttpConnectionParams.setConnectionTimeout(httpParams, 60000);// HttpConnectionParams.setSoTimeout(httpParams, 60000);//? HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUseExpectContinue(httpParams, false); httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES); DefaultHttpClient httpClient = new DefaultHttpClient(cm, httpParams); //httpClient.setCookieStore(null); httpClient.getCookieStore().clear(); httpClient.getCookieStore().getCookies().clear(); // httpClient.setHttpRequestRetryHandler(new HttpJsonClient().new HttpRequestRetry());//? return httpClient; }
From source file:org.jets3t.service.utils.RestUtils.java
/** * Initialises, or re-initialises, the underlying HttpConnectionManager and * HttpClient objects a service will use to communicate with an AWS service. * If proxy settings are specified in this service's {@link Jets3tProperties} object, * these settings will also be passed on to the underlying objects. *//*from w w w. j a v a 2s . c o m*/ public static HttpClient initHttpConnection(final JetS3tRequestAuthorizer requestAuthorizer, Jets3tProperties jets3tProperties, String userAgentDescription, CredentialsProvider credentialsProvider) { // Configure HttpClient properties based on Jets3t Properties. HttpParams params = createDefaultHttpParams(); params.setParameter(Jets3tProperties.JETS3T_PROPERTIES_ID, jets3tProperties); params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, jets3tProperties.getStringProperty( ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, ConnManagerFactory.class.getName())); HttpConnectionParams.setConnectionTimeout(params, jets3tProperties.getIntProperty("httpclient.connection-timeout-ms", 60000)); HttpConnectionParams.setSoTimeout(params, jets3tProperties.getIntProperty("httpclient.socket-timeout-ms", 60000)); HttpConnectionParams.setStaleCheckingEnabled(params, jets3tProperties.getBoolProperty("httpclient.stale-checking-enabled", true)); // Connection properties to take advantage of S3 window scaling. if (jets3tProperties.containsKey("httpclient.socket-receive-buffer")) { HttpConnectionParams.setSocketBufferSize(params, jets3tProperties.getIntProperty("httpclient.socket-receive-buffer", 0)); } HttpConnectionParams.setTcpNoDelay(params, true); // Set user agent string. String userAgent = jets3tProperties.getStringProperty("httpclient.useragent", null); if (userAgent == null) { userAgent = ServiceUtils.getUserAgentDescription(userAgentDescription); } if (log.isDebugEnabled()) { log.debug("Setting user agent string: " + userAgent); } HttpProtocolParams.setUserAgent(params, userAgent); boolean expectContinue = jets3tProperties.getBoolProperty("http.protocol.expect-continue", true); HttpProtocolParams.setUseExpectContinue(params, expectContinue); long connectionManagerTimeout = jets3tProperties.getLongProperty("httpclient.connection-manager-timeout", 0); ConnManagerParams.setTimeout(params, connectionManagerTimeout); DefaultHttpClient httpClient = new DefaultHttpClient(params); httpClient.setHttpRequestRetryHandler(new JetS3tRetryHandler( jets3tProperties.getIntProperty("httpclient.retry-max", 5), requestAuthorizer)); if (credentialsProvider != null) { if (log.isDebugEnabled()) { log.debug("Using credentials provider class: " + credentialsProvider.getClass().getName()); } httpClient.setCredentialsProvider(credentialsProvider); if (jets3tProperties.getBoolProperty("httpclient.authentication-preemptive", false)) { // Add as the very first interceptor in the protocol chain httpClient.addRequestInterceptor(new PreemptiveInterceptor(), 0); } } return httpClient; }
From source file:Main.java
public static DefaultHttpClient setHttpPostProxyParams(Context context) { Log.d(TAG, "ctx -- called "); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); //schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); HttpParams params = new BasicHttpParams(); /*Log.d(TAG, "----Add Proxy---"); HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(), Constants.PROXY_PORT);// ww w.ja v a 2 s . com params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);*/ /*if ((getWifiName(context).trim()).equalsIgnoreCase("secure-impact")) { Log.d(TAG, "----Add Proxy---"); HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(), Constants.PROXY_PORT); params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); }*/ 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); ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry); DefaultHttpClient mHttpClient = new DefaultHttpClient(cm, params); return mHttpClient; }
From source file:Main.java
public static DefaultHttpClient setHttpPostProxyParams(Context context, int connTimeout) { Log.d(TAG, "ctx, connTimeout -- called"); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); //schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); HttpParams params = new BasicHttpParams(); /*Log.d(TAG, "----Add Proxy---"); HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(), Constants.PROXY_PORT);//from w w w. j a va 2 s. c om params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);*/ /*if ((getWifiName(context).trim()).equalsIgnoreCase("secure-impact")) { Log.d(TAG, "----Add Proxy---"); HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(), Constants.PROXY_PORT); params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); }*/ 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); HttpConnectionParams.setConnectionTimeout(params, connTimeout); HttpConnectionParams.setSoTimeout(params, connTimeout); ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry); DefaultHttpClient mHttpClient = new DefaultHttpClient(cm, params); return mHttpClient; }
From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java
public static FanWallMessage postMessage(String msg, String imagePath, int parentId, int replyId, boolean withGps) { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpClient httpClient = new DefaultHttpClient(params); try {//w ww . j av a 2s. c om HttpPost httpPost = new HttpPost(Statics.BASE_URL + "/"); MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("app_id", new StringBody(com.appbuilder.sdk.android.Statics.appId, Charset.forName("UTF-8"))); multipartEntity.addPart("token", new StringBody(com.appbuilder.sdk.android.Statics.appToken, Charset.forName("UTF-8"))); multipartEntity.addPart("module_id", new StringBody(Statics.MODULE_ID, Charset.forName("UTF-8"))); // ? ? multipartEntity.addPart("parent_id", new StringBody(Integer.toString(parentId), Charset.forName("UTF-8"))); multipartEntity.addPart("reply_id", new StringBody(Integer.toString(replyId), Charset.forName("UTF-8"))); if (Authorization.getAuthorizedUser().getAccountType() == User.ACCOUNT_TYPES.FACEBOOK) { multipartEntity.addPart("account_type", new StringBody("facebook", Charset.forName("UTF-8"))); } else if (Authorization.getAuthorizedUser().getAccountType() == User.ACCOUNT_TYPES.TWITTER) { multipartEntity.addPart("account_type", new StringBody("twitter", Charset.forName("UTF-8"))); } else { multipartEntity.addPart("account_type", new StringBody("ibuildapp", Charset.forName("UTF-8"))); } multipartEntity.addPart("account_id", new StringBody(Authorization.getAuthorizedUser().getAccountId(), Charset.forName("UTF-8"))); multipartEntity.addPart("user_name", new StringBody(Authorization.getAuthorizedUser().getUserName(), Charset.forName("UTF-8"))); multipartEntity.addPart("user_avatar", new StringBody(Authorization.getAuthorizedUser().getAvatarUrl(), Charset.forName("UTF-8"))); if (withGps) { if (Statics.currentLocation != null) { multipartEntity.addPart("latitude", new StringBody(Statics.currentLocation.getLatitude() + "", Charset.forName("UTF-8"))); multipartEntity.addPart("longitude", new StringBody(Statics.currentLocation.getLongitude() + "", Charset.forName("UTF-8"))); } } multipartEntity.addPart("text", new StringBody(msg, Charset.forName("UTF-8"))); if (!TextUtils.isEmpty(imagePath)) { multipartEntity.addPart("images", new FileBody(new File(imagePath))); } httpPost.setEntity(multipartEntity); String resp = httpClient.execute(httpPost, new BasicResponseHandler()); return JSONParser.parseMessagesString(resp).get(0); } catch (Exception e) { return null; } }
From source file:uk.co.techblue.docusign.client.DocuSignClient.java
private static HttpClient getHttpClient() { if (client == null) { synchronized (DocuSignClient.class) { if (client == null) { ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(); int maxPerRoute = httpClientConfiguration.getDefaultMaxPerRoute(); cm.setDefaultMaxPerRoute(maxPerRoute); cm.setMaxTotal(maxPerRoute); client = new DefaultHttpClient(cm); int timeout = httpClientConfiguration.getTimeout(); String proxyHost = httpClientConfiguration.getProxyHost(); HttpParams params = client.getParams(); // Allowable time between packets HttpConnectionParams.setSoTimeout(params, timeout); // Allowable time to get a connection HttpConnectionParams.setConnectionTimeout(params, timeout); // Configure proxy info if necessary and defined if (proxyHost != null && !proxyHost.equals("")) { // Configure the host and port int port = httpClientConfiguration.getProxyPort(); HttpHost proxy = new HttpHost(proxyHost, port); params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); }/*from w w w. j av a 2 s .c o m*/ } } } if (logger.isDebugEnabled()) { logger.info("connections: " + ((ThreadSafeClientConnManager) client.getConnectionManager()).getConnectionsInPool()); } return client; }
From source file:com.fanfou.app.opensource.util.NetworkHelper.java
/** * ????/*from ww w.jav a 2 s . c o m*/ * * @param context * @param httpParams */ private static final void checkAndSetProxy(final Context context, final HttpParams httpParams) { boolean needCheckProxy = true; final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if ((networkInfo == null) || NetworkHelper.WIFI.equalsIgnoreCase(networkInfo.getTypeName()) || (networkInfo.getExtraInfo() == null)) { needCheckProxy = false; } if (needCheckProxy) { final String typeName = networkInfo.getExtraInfo(); if (NetworkHelper.MOBILE_CTWAP.equalsIgnoreCase(typeName)) { final HttpHost proxy = new HttpHost("10.0.0.200", 80); httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } else if (NetworkHelper.MOBILE_CMWAP.equalsIgnoreCase(typeName) || NetworkHelper.MOBILE_UNIWAP.equalsIgnoreCase(typeName) || NetworkHelper.MOBILE_3GWAP.equalsIgnoreCase(typeName)) { final HttpHost proxy = new HttpHost("10.0.0.172", 80); httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } } // String defaultProxyHost = android.net.Proxy.getDefaultHost(); // int defaultProxyPort = android.net.Proxy.getDefaultPort(); // if (defaultProxyHost != null && defaultProxyHost.length() > 0 // && defaultProxyPort > 0) { // HttpHost proxy = new HttpHost(defaultProxyHost, defaultProxyPort); // httpParams.setParameter(ConnRouteParams.DEFAULT_PROXY, proxy); // } }