private static HttpClient getNewHttpClient() {
    try {
      KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
      trustStore.load(null, null);

      SSLSocketFactory sf = new MySSLSocketFactory(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);
      HttpConnectionParams.setConnectionTimeout(params, 10000);
      HttpConnectionParams.setSoTimeout(params, 20000);

      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();
    }
  }
  protected @Nonnull HttpClient getClient(String url, boolean multipart) throws InternalException {
    ProviderContext ctx = provider.getContext();

    if (ctx == null) {
      throw new InternalException("No context was specified for this request");
    }
    boolean ssl = url.startsWith("https");
    HttpParams params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    if (!multipart) {
      HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    }
    HttpProtocolParams.setUserAgent(params, "Dasein Cloud");

    Properties p = ctx.getCustomProperties();

    if (p != null) {
      String proxyHost = p.getProperty("proxyHost");
      String proxyPort = p.getProperty("proxyPort");

      if (proxyHost != null) {
        int port = 0;

        if (proxyPort != null && proxyPort.length() > 0) {
          port = Integer.parseInt(proxyPort);
        }
        params.setParameter(
            ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, port, ssl ? "https" : "http"));
      }
    }
    return new DefaultHttpClient(params);
  }
  private static AndroidHttpClient createHttpClient(Context context) {

    // Get Shared Preferences and User Defined User Agent for MMS
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    mUserAgent = prefs.getString(MessagingPreferenceActivity.USER_AGENT, MmsConfig.getUserAgent());
    if (mUserAgent == null || mUserAgent.equals("") || mUserAgent.equals("default")) {
      mUserAgent = MmsConfig.getUserAgent();
    } else if (mUserAgent.equals("custom")) {
      mUserAgent =
          prefs.getString(MessagingPreferenceActivity.USER_AGENT_CUSTOM, MmsConfig.getUserAgent());
    }

    AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);

    HttpParams params = client.getParams();
    HttpProtocolParams.setContentCharset(params, "UTF-8");

    // set the socket timeout
    int soTimeout = MmsConfig.getHttpSocketTimeout();

    if (Log.isLoggable(LogTag.TRANSACTION, Log.DEBUG)) {
      Log.d(
          TAG,
          "[HttpUtils] createHttpClient w/ socket timeout "
              + soTimeout
              + " ms, "
              + ", UA="
              + mUserAgent);
    }
    HttpConnectionParams.setSoTimeout(params, soTimeout);
    return client;
  }
Beispiel #4
0
  static {
    httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();

    // set the time out of the connection/ socket. and the cache size
    HttpConnectionParams.setConnectionTimeout(params, 30 * 1000);
    HttpConnectionParams.setSoTimeout(params, 30 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // set the connection manager factory
    params.setParameter(
        ClientPNames.CONNECTION_MANAGER_FACTORY,
        new ClientConnectionManagerFactory() {
          public ClientConnectionManager newInstance(
              HttpParams params, SchemeRegistry schemeRegistry) {
            return new ThreadSafeClientConnManager(params, schemeRegistry);
          }
        });

    // set the redirect, default is true
    HttpClientParams.setRedirecting(params, true);

    // set the client verion
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    // set user-agent
    HttpProtocolParams.setUserAgent(params, "eoeWIKI_Android/0.9");
    // set the charset
    HttpProtocolParams.setHttpElementCharset(params, HTTP.UTF_8);
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    // set not activate Expect:100-Continue
    HttpProtocolParams.setUseExpectContinue(params, false);

    // set the version of the cookie
    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);
  }
Beispiel #5
0
 public 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);
     HttpProtocolParams.setUserAgent(
         params,
         "Mozilla/5.0 (Windows; Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36");
     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);
     @SuppressWarnings("resource")
     DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
     httpClient.setCookieStore(new BasicCookieStore());
     httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
     httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
     return new DefaultHttpClient(ccm, params);
   } catch (Exception e) {
     return new DefaultHttpClient();
   }
 }
Beispiel #6
0
 private static DefaultHttpClient createHttpsClient() {
   DefaultHttpClient defaulthttpclient = new DefaultHttpClient();
   DefaultHttpClient defaulthttpclient1;
   try {
     KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
     keystore.load(null, null);
     CustomSSLSocketFactory customsslsocketfactory = new CustomSSLSocketFactory(keystore);
     customsslsocketfactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
     BasicHttpParams basichttpparams = new BasicHttpParams();
     HttpProtocolParams.setVersion(basichttpparams, HttpVersion.HTTP_1_1);
     HttpProtocolParams.setContentCharset(basichttpparams, "utf-8");
     HttpProtocolParams.setUseExpectContinue(basichttpparams, false);
     HttpConnectionParams.setConnectionTimeout(basichttpparams, HttpConst.NETWORK_TIME_OUT);
     HttpConnectionParams.setSoTimeout(basichttpparams, HttpConst.NETWORK_TIME_OUT);
     SchemeRegistry schemeregistry = new SchemeRegistry();
     schemeregistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
     schemeregistry.register(new Scheme("https", customsslsocketfactory, 443));
     defaulthttpclient1 =
         new DefaultHttpClient(
             new ThreadSafeClientConnManager(basichttpparams, schemeregistry), basichttpparams);
     defaulthttpclient1
         .getCredentialsProvider()
         .setCredentials(
             new AuthScope(null, -1), new UsernamePasswordCredentials("admin", "admin"));
     defaulthttpclient = defaulthttpclient1;
   } catch (Exception e) {
     e.printStackTrace();
   }
   return defaulthttpclient;
 }
  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, 1000);
      HttpConnectionParams.setConnectionTimeout(params, 2000);
      HttpConnectionParams.setSoTimeout(params, 4000);

      // 设置我们的HttpClient支持HTTP和HTTPS两种模式
      SchemeRegistry schReg = new SchemeRegistry();
      schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
      schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

      // 使用线程安全的连接管理来创建HttpClient
      ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
      customerHttpClient = new DefaultHttpClient(conMgr, params);
    }
    return customerHttpClient;
  }
 private HttpClient getHttpClient() {
   if (null == customerHttpClient) {
     HttpParams params = new BasicHttpParams();
     // 设置一些基本参数
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
     HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
     HttpProtocolParams.setUseExpectContinue(params, true);
     HttpProtocolParams.setUserAgent(params, "Linux; Android 2.2.1;zh-CN; itkt_Android");
     //			HttpClientParams.setRedirecting(params, true);
     //		    HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
     // 设置最大连接数
     ConnManagerParams.setMaxTotalConnections(params, MAX_TOTAL_CONNECTIONS);
     // 设置获取连接的最大等待时间
     ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
     // 设置每个路由最大连接数
     ConnPerRouteBean connPerRoute = new ConnPerRouteBean(MAX_ROUTE_CONNECTIONS);
     ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
     // 设置连接超时时间
     HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT);
     // 设置读取超时时间
     HttpConnectionParams.setSoTimeout(params, READ_TIMEOUT);
     try {
       KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
       SchemeRegistry schReg = new SchemeRegistry();
       schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
       // 使用线程安全的连接管理来创建HttpClient
       ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
       customerHttpClient = new DefaultHttpClient(conMgr, params);
       customerHttpClient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return customerHttpClient;
 }
Beispiel #9
0
  protected DefaultHttpClient getHttpClient() {
    if (httpClient == null) {
      HttpParams httpParams = new BasicHttpParams();
      HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
      HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);
      HttpProtocolParams.setUseExpectContinue(
          httpParams, false); // some webservers have problems if this is set to true
      ConnManagerParams.setMaxTotalConnections(httpParams, MAX_TOTAL_CONNECTIONS);
      HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
      HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT);

      //            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, new
      // HttpHost("192.168.16.180", 8888, "http"));
      httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

      SchemeRegistry schemeRegistry = new SchemeRegistry();
      schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
      schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

      ClientConnectionManager connectionManager =
          new ThreadSafeClientConnManager(httpParams, schemeRegistry);

      httpClient = new DefaultHttpClient(connectionManager, httpParams);
    }

    return httpClient;
  }
    /**
     * Static method to initialize HTTP client. Notice the double instance == null check. By
     * checking in and outside of the synchronization, this method is threadsafe. It ensures the
     * singleton is only instantiated once.
     *
     * @return
     */
    public HttpClient getInstance() {
      if (INSTANCE == null) {
        synchronized (HttpConnectionManager.class) {
          if (INSTANCE == null) {
            final HttpParams params = new BasicHttpParams();
            final SchemeRegistry registry = new SchemeRegistry();
            final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();

            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, "utf-8");
            HttpProtocolParams.setUserAgent(params, "Basedroid");

            sslSocketFactory.setHostnameVerifier(
                SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sslSocketFactory, 443));

            final ClientConnectionManager manager =
                new ThreadSafeClientConnManager(params, registry);
            INSTANCE = new DefaultHttpClient(manager, params);
          }
        }
      }
      return INSTANCE;
    }
Beispiel #11
0
  /**
   * Create HttpClient based on HTTP or HTTPS protocol that is parsed from url parameter. With HTTPS
   * protocol, we accept all SSL certificates.
   *
   * @param url
   * @param params
   * @return
   */
  protected HttpClient createHttpClient(String url, HttpParams params) {
    if ((url.toLowerCase().startsWith("https"))) {
      // HTTPS process
      try {

        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new FMySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        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(params);
      }
    } else {
      // HTTP process
      return new DefaultHttpClient(params);
    }
  }
  public HttpClient4Sender() {
    clientParams.setParameter(
        CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
    clientParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_READ_TIMEOUT_MILLISECONDS);

    HttpProtocolParams.setVersion(clientParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(clientParams, "UTF-8");
  }
  /** LDR4JのHTTPクライアントを構築します。 */
  protected AbstractLDRHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    HttpProtocolParams.setUserAgent(params, LDR4J.TITLE + "/" + LDR4J.VERSION);

    httpClient = new DefaultHttpClient(params);
  }
    @Override
    protected String doInBackground(String... params) {
      // TODO Auto-generated method stub

      String response = null;

      try {

        SSLSocketFactory sslFactory =
            new CustomSSLSocketFactory(
                CustomSSLSocketFactory.getKeyStoreForCertificate(
                    getApplicationContext().getResources().getAssets().open("new_server.crt")));
        // sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        // to be used in case of bypassing certificates

        // Enable HTTP parameters */
        HttpParams params4 = new BasicHttpParams();
        HttpProtocolParams.setVersion(params4, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params4, HTTP.UTF_8);

        // Register the HTTP and HTTPS Protocols. For HTTPS, register
        // our custom SSL Factory object.
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sslFactory, 443));

        // Create a new connection manager using the newly created
        // registry and then create a new HTTP client
        // using this connection manager
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params4, registry);
        DefaultHttpClient client = new DefaultHttpClient(ccm, params4);
        HttpPost httppostreq = new HttpPost(params[0]);
        StringEntity se = new StringEntity(params[1]);
        se.setContentType("application/json;charset=UTF-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
        httppostreq.setEntity(se);
        HttpResponse httpresponse = client.execute(httppostreq);
        httpresponse.getStatusLine().getStatusCode();

        HttpEntity httpEntity = httpresponse.getEntity();
        response = EntityUtils.toString(httpEntity);
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      } catch (ClientProtocolException e) {
        e.printStackTrace();
      } catch (SSLHandshakeException e) {
        e.printStackTrace();
      } catch (SSLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        // e.printStackTrace();
        System.out.println(e);
      } catch (Exception e) {
        e.printStackTrace();
      }

      return response;
    }
  // 每次都返回新的HttpClient实例
  public static HttpClient getNewInstance(Context mContext) {
    HttpClient newInstance;

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);

    // 自定义三个timeout参数

    /*
     * 1.set a timeout for the connection manager,it defines how long we
     * should wait to get a connection out of the connection pool managed by
     * the connection manager
     */
    ConnManagerParams.setTimeout(params, 5000);

    /*
     * 2.The second timeout value defines how long we should wait to make a
     * connection over the network to the server on the other end
     */
    HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);

    /*
     * 3.we set a socket timeout value to 4 seconds to define how long we
     * should wait to get data back for our request.
     */
    HttpConnectionParams.setSoTimeout(params, TIMEOUT_SOCKET);

    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);

    newInstance = new DefaultHttpClient(conMgr, params);

    switch (checkNetworkType(mContext)) {
      case TYPE_CT_WAP:
        {
          // 通过代理解决中国移动联通GPRS中wap无法访问的问题
          HttpHost proxy = new HttpHost("10.0.0.200", 80, "http");
          newInstance.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
          Logs.v("当前网络类型为cm_cu_wap,设置代理10.0.0.200访问www");
        }
        break;
      case TYPE_CM_CU_WAP:
        {
          // 通过代理解决中国移动联通GPRS中wap无法访问的问题
          HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
          newInstance.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
          Logs.v("当前网络类型为cm_cu_wap,设置代理10.0.0.172访问www");
        }
        break;
    }
    return newInstance;
  }
 private FabHttpConnection()
 {
   this.params.setBooleanParameter("http.protocol.expect-continue", false);
   this.params.setBooleanParameter("http.connection.stalecheck", false);
   this.params.setIntParameter("http.socket.buffer-size", 8192);
   HttpProtocolParams.setVersion(this.params, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(this.params, "UTF-8");
   this.client = new DefaultHttpClient(this.params);
   this.client.getConnectionManager().closeExpiredConnections();
 }
Beispiel #17
0
 private HttpClient createHttpClient() {
   HttpParams params = new BasicHttpParams();
   HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
   HttpProtocolParams.setUseExpectContinue(params, true);
   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);
   return new DefaultHttpClient(conMgr, params);
 }
 static {
   HttpParams params = new BasicHttpParams();
   HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(params, "UTF-8");
   HttpProtocolParams.setUserAgent(params, WeaveConstants.USER_AGENT);
   HttpProtocolParams.setUseExpectContinue(params, false);
   //    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
   params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
   params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
   sm_httpParams = params;
 }
  private static HttpClient getHttpClient() {

    HttpParams httpParameters = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
    HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);
    mHttpClient = new DefaultHttpClient(httpParameters);
    mHttpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    mHttpClient.getParams().setParameter("http.socket.timeout", new Integer(2000));
    mHttpClient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
    httpParameters.setBooleanParameter("http.protocol.expect-continue", false);

    return mHttpClient;
  }
  public static synchronized HttpClient getInstance(Context context) {
    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, System.getProperties().getProperty("http.agent") + " Mozilla/5.0 Firefox/26.0");

      // 超时设置
      /* 从连接池中取连接的超时时间 */
      ConnManagerParams.setTimeout(params, 10 * 1000);

      /* 连接超时 */
      HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);

      /* 请求超时 */
      HttpConnectionParams.setSoTimeout(params, 10 * 1000);

      // 支持http和https两种模式
      SchemeRegistry schReg = new SchemeRegistry();
      schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
      schReg.register(new Scheme("https", getSSLSocketFactory(), 443));

      // 使用线程安全的连接管理来创建HttpClient
      ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

      customerHttpClient = new DefaultHttpClient(conMgr, params);
      customerHttpClient.setHttpRequestRetryHandler(requestRetryHandler);
      ConnectivityManager manager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

      NetworkInfo networkinfo = manager.getActiveNetworkInfo();
      String net = networkinfo != null ? networkinfo.getExtraInfo() : null;

      // wifi的值为空
      if (!TextUtils.isEmpty(net)) {
        String proxyHost = getDefaultHost();

        if (!TextUtils.isEmpty(proxyHost)) {
          HttpHost proxy = new HttpHost(proxyHost, getDefaultPort(), "http");

          customerHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
      }
    }
    return customerHttpClient;
  }
 /* constructor */
 public JsonFeedGetter(Context c, int mode) {
   mode_ = mode;
   context_ = c;
   final HttpParams httpParams = new BasicHttpParams();
   HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
   HttpConnectionParams.setConnectionTimeout(httpParams, connection_Timeout);
   HttpConnectionParams.setSoTimeout(httpParams, connection_Timeout);
   final SchemeRegistry schemeRegistry = new SchemeRegistry();
   schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
   httpClient_ =
       new DefaultHttpClient(
           new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
 }
  @Override
  public List<BusinessInfoVo> query(BusinessCondition businessCondition, int pageNo)
      throws Exception {
    List<BusinessInfoVo> list = null;
    // post
    String uri = "http://192.168.3.3:8080/ba/businessinfoAction_query.action";

    // 参数设置
    HttpParams params = new BasicHttpParams();
    // 通过params设置请求的字符集
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    // 设置客户端与服务端连接的超时时间(还没有连接到服务器) ConnectTimeoutException
    HttpConnectionParams.setConnectionTimeout(params, 3000);
    // 设置服务器的响应时间(已经连接到服务器了,对话后的响应时间) SocketTimeoutException
    HttpConnectionParams.setSoTimeout(params, 3000);

    // 设置https与http都可以访问
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(new Scheme("https", PlainSocketFactory.getSocketFactory(), 433));
    sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager conn = new ThreadSafeClientConnManager(params, sr);

    HttpClient client = new DefaultHttpClient(conn, params);
    HttpPost post = new HttpPost(uri);
    NameValuePair businessInfoJson =
        new BasicNameValuePair("businessCondition", JsonHepler.toJSON(businessCondition));
    NameValuePair pageNoValue = new BasicNameValuePair("pageNo", pageNo + "");

    List<NameValuePair> paramters = new ArrayList<NameValuePair>();
    paramters.add(businessInfoJson);
    paramters.add(pageNoValue);
    post.setEntity(new UrlEncodedFormEntity(paramters, HTTP.UTF_8));
    HttpResponse response = client.execute(post);

    int status = response.getStatusLine().getStatusCode();
    if (status != HttpStatus.SC_OK) {
      throw new ServiceRulesException(String.format(Constant.REQUEST_ERROR, "查询业务信息", status));
    }

    String result = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

    if (result != null && !"null".equals(result) && "" != result) {
      list =
          (List<BusinessInfoVo>)
              JsonHepler.parseCollection(result, ArrayList.class, BusinessInfoVo.class);
    } else {
      throw new ServiceRulesException(String.format(Constant.RESPOSE_ERROR, "查询业务信息"));
    }
    return list;
  }
Beispiel #23
0
 private static void setupHttpClient() {
   BasicHttpParams httpParams = new BasicHttpParams();
   ConnManagerParams.setTimeout(httpParams, CONNECTION_TIMEOUT);
   ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(MAX_CONNECTIONS));
   ConnManagerParams.setMaxTotalConnections(httpParams, MAX_CONNECTIONS);
   HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setUserAgent(httpParams, HTTP_USER_AGENT);
   SchemeRegistry schemeRegistry = new SchemeRegistry();
   schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
   // schemeRegistry.register(new Scheme("https", new
   // EasySSLProtocolSocketFactory(), 443));
   ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
   HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);
   httpClient = new DefaultHttpClient(cm, httpParams);
 }
  /* Create HttpClient Instance with Application Settings Specified */
  private HttpClient getHttpClient(HttpRequestType requestType) {
    HttpParams httpParams = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);
    HttpProtocolParams.setHttpElementCharset(httpParams, HTTP.UTF_8);

    if (requestType == HttpRequestType.POST) {
      HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_POST_CONNECTION_TIMEOUT);
      HttpConnectionParams.setSoTimeout(httpParams, HTTP_POST_WAITING_FOR_DATA_TIMEOUT);
    } else {
      HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_GET_CONNECTION_TIMEOUT);
      HttpConnectionParams.setSoTimeout(httpParams, HTTP_GET_WAITING_FOR_DATA_TIMEOUT);
    }
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
    httpclient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
    return httpclient;
  }
  /*
   * Special thanks to Tobias Knell at
   * http://blog.synyx.de/2010/06/android-and-self-signed-ssl-certificates/
   * The following method prepares for the HTTPS connection.
   */
  private void networkSetup() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    // HTTP scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // HTTPS scheme
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

    params = new BasicHttpParams();
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf8");
    clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
  }
  @Override
  protected HttpParams createHttpParams() {
    HttpParams params = new SyncBasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // determine the release version from packaged version info
    final VersionInfo vi =
        VersionInfo.loadVersionInfo("org.apache.http.client", getClass().getClassLoader());
    final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
    HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/" + release + " (java 1.5)");

    return params;
  }
 public static DefaultHttpClient getNewHttpClient(KeyStore paramKeyStore)
 {
   try
   {
     MySSLSocketFactory localMySSLSocketFactory = new MySSLSocketFactory(paramKeyStore);
     SchemeRegistry localSchemeRegistry = new SchemeRegistry();
     localSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
     localSchemeRegistry.register(new Scheme("https", localMySSLSocketFactory, 443));
     BasicHttpParams localBasicHttpParams = new BasicHttpParams();
     HttpProtocolParams.setVersion(localBasicHttpParams, HttpVersion.HTTP_1_1);
     HttpProtocolParams.setContentCharset(localBasicHttpParams, "UTF-8");
     DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(localBasicHttpParams, localSchemeRegistry), localBasicHttpParams);
     return localDefaultHttpClient;
   }
   catch (Exception localException) {}
   return new DefaultHttpClient();
 }
Beispiel #28
0
  private HttpClient createHttpClient() {
    if (mHttpClient != null) return mHttpClient;
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    HttpProtocolParams.setUseExpectContinue(params, true);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SSLSocketFactory sf = getSSLSocketFactory();
    schReg.register(new Scheme("https", sf, 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    mHttpClient = new DefaultHttpClient(conMgr, params);

    return mHttpClient;
  }
  @Override
  public BusinessInfo update(BusinessInfo businessInfo) throws Exception {
    businessInfo.setCreateTime(new Date());
    businessInfo.setOverTime(new Date());

    // post
    String uri = "http://192.168.3.3:8080/ba/businessinfoAction_update.action";

    // 参数设置
    HttpParams params = new BasicHttpParams();
    // 通过params设置请求的字符集
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    // 设置客户端与服务端连接的超时时间(还没有连接到服务器) ConnectTimeoutException
    HttpConnectionParams.setConnectionTimeout(params, 3000);
    // 设置服务器的响应时间(已经连接到服务器了,对话后的响应时间) SocketTimeoutException
    HttpConnectionParams.setSoTimeout(params, 3000);

    // 设置https与http都可以访问
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(new Scheme("https", PlainSocketFactory.getSocketFactory(), 433));
    sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager conn = new ThreadSafeClientConnManager(params, sr);

    HttpClient client = new DefaultHttpClient(conn, params);
    HttpPost post = new HttpPost(uri);
    NameValuePair businessInfoJson =
        new BasicNameValuePair("businessInfo", JsonHepler.toJSON(businessInfo));
    List<NameValuePair> paramters = new ArrayList<NameValuePair>();
    paramters.add(businessInfoJson);
    post.setEntity(new UrlEncodedFormEntity(paramters, HTTP.UTF_8));
    HttpResponse response = client.execute(post);

    int status = response.getStatusLine().getStatusCode();
    if (status != HttpStatus.SC_OK) {
      throw new ServiceRulesException(String.format(Constant.REQUEST_ERROR, "修改业务信息", status));
    }

    String result = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

    if (null != result && !"".equals(result)) {
      businessInfo.setBusinessID(Integer.valueOf(result));
      return businessInfo;
    } else {
      throw new ServiceRulesException(String.format(Constant.RESPOSE_ERROR, "修改业务"));
    }
  }
  /** Create the default HTTP protocol parameters. */
  private static final HttpParams createHttpParams(Context mContext) {
    final HttpParams params = new BasicHttpParams();

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf-8");
    // 加入代理
    HttpUtils.fillProxy(mContext, params);
    // 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, TIMEOUT * 1000);
    HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    return params;
  }