HttpClient proxy settings based on system configuration

sponsored links
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import android.content.Context;
import android.net.Proxy;
import android.text.TextUtils;
import android.util.Log;

public final class HttpClientFactory {
        private static final String TAG = HttpClientFactory.class.getSimpleName();
        
        private static final int BUFFER_SIZE = 8192;
        private static final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB6 (.NET CLR 3.5.30729)";
        private static final int CONN_TIME_OUT = 20 * 1000;
        private static final int READ_TIME_OUT = 20 * 1000;

        private HttpClientFactory() {
                super();
                // TODO Auto-generated constructor stub
        }
        
        public static HttpClient createHttpClient(Context context) {
                
                //  Create HttpParams to set   HTTP  Parameters  ( This section is not required  )
                HttpParams httpParams = new BasicHttpParams();

                //  Setting connection timeouts and Socket timeout  , As well as the Socket buffer size  
                HttpConnectionParams.setConnectionTimeout(httpParams, CONN_TIME_OUT);
                HttpConnectionParams.setSoTimeout(httpParams, READ_TIME_OUT);
                HttpConnectionParams.setSocketBufferSize(httpParams, BUFFER_SIZE);

                //  Set the redirect, defaults to   true
                HttpClientParams.setRedirecting(httpParams, true);
                
                //  Detect proxy settings  
                String proxyHost = Proxy.getHost(context);
                int proxyPort = Proxy.getPort(context);
                
                Log.d(TAG, "[createHttpClient] proxyHost = " + proxyHost);
                Log.d(TAG, "[createHttpClient] proxyPort = " + proxyPort);
                
                boolean isWifiConnected = Utils.isWifiConnected(context);
                boolean isMobileConnected = Utils.isMobileConnected(context);
                
                Log.d(TAG, "[createHttpClient] isWifiConnected = " + isWifiConnected);
                Log.d(TAG, "[createHttpClient] isMobileConnected = " + isMobileConnected);
                
                if (!isWifiConnected && !TextUtils.isEmpty(proxyHost)) {
                        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
                Log.d(TAG, "Set proxy: host: " + proxyHost + " port:" + proxyPort);
                httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                }

                //  Set   user agent
                String userAgent = USER_AGENT;
                HttpProtocolParams.setUserAgent(httpParams, userAgent);

                HttpClient httpClient = new DefaultHttpClient(httpParams);
                
                return httpClient;
        }
}
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of HttpClient proxy settings based on system configuration

  • Maven 2.0: Compile. Test. Deployment. Run

    <url> http://maven.apache.org </ url> <dependencies> <dependency> <groupId> junit </ groupId> <artifactId> junit </ artifactId> <version> 3.8.1 </ version> <scope> test </ scope> <

  • Struts2 Spring Hibernate integration of easy

    1. Add Spring 2.0 in Libraries Choose the following four jar, and configure the / WEB-INF/lib under Spring2.0 AOP Libraries Spring2.0 Core Libraries Spring2.0 Persistence Core Libraries Spring2.0 WEb Libraries At the same time, the applicationContext ...

  • Struts2 Spring Hibernate's easy to integrate

    1. Add Spring 2.0 in Libraries Choose the following four jar, and configure the / WEB-INF/lib under Spring2.0 AOP Libraries Spring2.0 Core Libraries Spring2.0 Persistence Core Libraries Spring2.0 WEb Libraries At the same time, the applicationContext ...

  • log4j easy application in java

    JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu

  • JDBC example of a long time do not have JDBC forgot

    A back-up here to stay. The first: The second:

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • spring struts2.0 hibernate bug killer 1

    exception There is no Action mapped for namespace / and action name checkLogin. - [Unknown location] com.opensymphony.xwork2.DefaultActionProxy.prepare (DefaultActionProxy.java: 186) org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries