HttpClient proxy settings based on system configuration

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;
        }
}
分类:Mobile 时间:2010-10-13 人气:348
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.384 (s). 14 q(s)