Example usage for org.apache.http.params CoreConnectionPNames SO_TIMEOUT

List of usage examples for org.apache.http.params CoreConnectionPNames SO_TIMEOUT

Introduction

In this page you can find the example usage for org.apache.http.params CoreConnectionPNames SO_TIMEOUT.

Prototype

String SO_TIMEOUT

To view the source code for org.apache.http.params CoreConnectionPNames SO_TIMEOUT.

Click Source Link

Usage

From source file:com.xiaomi.infra.galaxy.sds.client.SdsTHttpClient.java

public void setReadTimeout(int timeout) {
    readTimeout_ = timeout;/*from w  w w. j  a  v  a  2 s  . c  o  m*/
    if (null != this.client) {
        // WARNING, this modifies the HttpClient params, this might have an impact elsewhere if the
        // same HttpClient is used for something else.
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout_);
    }
}

From source file:com.tiaoin.crawl.plugin.util.PageFetcherImpl.java

/**
 * client?Header?Cookie//from   w w  w  .ja v  a 2  s  .  co  m
 * @param aconfig
 * @param cookies
 */
public void init(Site site) {
    //System.out.println(site.toString());
    if (null != site.getHeaders() && site.getHeaders().getHeader() != null) {
        for (com.tiaoin.crawl.core.xml.Header header : site.getHeaders().getHeader()) {
            this.addHeader(header.getName(), header.getValue());
        }
    }
    if (null != site.getCookies() && site.getCookies().getCookie() != null) {
        for (com.tiaoin.crawl.core.xml.Cookie cookie : site.getCookies().getCookie()) {
            this.addCookie(cookie.getName(), cookie.getValue(), cookie.getHost(), cookie.getPath());
        }
    }

    //HTTP?
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

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

    if (config.isIncludeHttpsPages())
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());

    httpClient = new DefaultHttpClient(connectionManager, params);
    httpClient.getParams().setIntParameter("http.socket.timeout", 15000);
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);

    //?
    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    //?GZIP
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });
}

From source file:NIO_HTTP_Client.java

public NIO_HTTP_Client(String user_agent, HttpRequestExecutionHandler request_handler,
        EventListener connection_listener) throws Exception {

    // Construct the long-lived HTTP parameters.
    HttpParams parameters = new BasicHttpParams();
    parameters/*from ww  w . jav a2  s.  com*/
            // Socket data timeout is 5,000 milliseconds (5 seconds).
            .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            // Maximum time allowed for connection establishment is 10,00 milliseconds (10 seconds).
            .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
            // Socket buffer size is 8 kB.
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            // Don't bother to check for stale TCP connections.
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            // Don't use Nagle's algorithm (in other words minimize latency).
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            // Set the user agent string that the client sends to the server.
            .setParameter(CoreProtocolPNames.USER_AGENT, user_agent);

    // Construct the core HTTP request processor.
    BasicHttpProcessor http_processor = new BasicHttpProcessor();
    // Add Content-Length header to request where appropriate.
    http_processor.addInterceptor(new RequestContent());
    // Always include Host header in requests.
    http_processor.addInterceptor(new RequestTargetHost());
    // Maintain connection keep-alive by default.
    http_processor.addInterceptor(new RequestConnControl());
    // Include user agent information in each request.
    http_processor.addInterceptor(new RequestUserAgent());

    // Allocate an HTTP client handler.
    BufferingHttpClientHandler client_handler = new BufferingHttpClientHandler(http_processor, // Basic HTTP Processor.
            request_handler, new DefaultConnectionReuseStrategy(), parameters);
    client_handler.setEventListener(connection_listener);

    // Use two worker threads for the IO reactor.
    io_reactor = new DefaultConnectingIOReactor(2, parameters);
    io_event_dispatch = new DefaultClientIOEventDispatch(client_handler, parameters);

    //    t = new Thread(new Runnable() {
    //             public void run() {
    //                 try {
    //                     io_reactor.execute(io_event_dispatch);
    //                 } catch (InterruptedIOException ex) {
    //                     System.err.println("Interrupted");
    //                 } catch (IOException e) {
    //                     System.err.println("I/O error: " + e.getMessage());
    //           Throwable c = e.getCause();
    //           if (c != null) {
    //          System.err.println("Cause: " + c.toString());
    //           }else {
    //          System.err.println("Cause: unknown");
    //           }
    //                 }
    //                 System.out.println("Shutdown");
    //             }
    //    });
    //    t.start();
}

From source file:edu.harvard.liblab.ecru.SolrClient.java

/**
 * Creates a new HttpClient. If keyStorePath and/or trustStorePath are set, a secure client will
 * be created by reading in the keyStore and/or trustStore. In addition, system keys will also be 
 * included (i.e. those specified in the JRE).
 * //from   w  ww.java  2s  . c o  m
 * Most of the code below is "borrowed" from edu.harvard.hul.ois.drs2.callservice.ServiceClient
 * 
 * @return  an HttpClient ready to roll!
 */
protected HttpClient createHttpClient() throws SolrClientException {
    // turn on SSL logging, according to Log4j settings

    DefaultHttpClient httpClient = new DefaultHttpClient();

    // set parameters
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout);
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
    httpClient.getParams().setParameter(ClientPNames.HANDLE_AUTHENTICATION, true);

    return httpClient;
}

From source file:com.pipi.studio.dev.net.AsyncHttpGet.java

@Override
public void run() {
    String ret = "";
    try {//www.  j  a v  a 2  s.c o m
        if (parameter != null && parameter.size() > 0) {
            StringBuilder bulider = new StringBuilder();
            for (RequestParameter p : parameter) {
                if (bulider.length() != 0) {
                    bulider.append("&");
                }

                bulider.append(Utils.encode(p.getName()));
                bulider.append("=");
                bulider.append(Utils.encode(p.getValue()));
            }
            url += "?" + bulider.toString();
        }
        LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet  request to url :" + url);
        request = new HttpGet(url);
        /*if(Constants.isGzip){
        request.addHeader("Accept-Encoding", "gzip");
          }else{
        request.addHeader("Accept-Encoding", "default");
          }*/
        //  
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
        // ? 
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            bis.mark(2);
            // ??
            byte[] header = new byte[2];
            int result = bis.read(header);
            // reset??
            bis.reset();
            // ?GZIP?
            int headerData = getShort(header);
            // Gzip ? ? 0x1f8b
            if (result != -1 && headerData == 0x1f8b) {
                LogUtil.d("HttpTask", " use GZIPInputStream  ");
                is = new GZIPInputStream(bis);
            } else {
                LogUtil.d("HttpTask", " not use GZIPInputStream");
                is = bis;
            }
            InputStreamReader reader = new InputStreamReader(is, "utf-8");
            char[] data = new char[100];
            int readSize;
            StringBuffer sb = new StringBuffer();
            while ((readSize = reader.read(data)) > 0) {
                sb.append(data, 0, readSize);
            }
            ret = sb.toString();
            bis.close();
            reader.close();

            //            ByteArrayOutputStream content = new ByteArrayOutputStream();
            //            response.getEntity().writeTo(content);
            //            ret = new String(content.toByteArray()).trim();
            //            content.close();
        } else {
            RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                    "??,??" + statusCode);
            ret = ErrorUtil.errorJson("ERROR.HTTP.001", exception.getMessage());
        }

        LogUtil.d(AsyncHttpGet.class.getName(), "AsyncHttpGet  request to url :" + url + "  finished !");

    } catch (java.lang.IllegalArgumentException e) {

        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                Constants.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("ERROR.HTTP.002", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpGet  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (org.apache.http.conn.ConnectTimeoutException e) {
        RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION,
                Constants.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("ERROR.HTTP.003", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpGet  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (java.net.SocketTimeoutException e) {
        RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION,
                Constants.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("ERROR.HTTP.004", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpGet  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        RequestException exception = new RequestException(RequestException.UNSUPPORTED_ENCODEING_EXCEPTION,
                "?");
        ret = ErrorUtil.errorJson("ERROR.HTTP.005", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpGet  request to url :" + url + "  UnsupportedEncodingException  " + e.getMessage());
    } catch (org.apache.http.conn.HttpHostConnectException e) {
        RequestException exception = new RequestException(RequestException.CONNECT_EXCEPTION,
                Constants.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("ERROR.HTTP.006", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpGet  request to url :" + url + "  HttpHostConnectException  " + e.getMessage());
    } catch (ClientProtocolException e) {
        RequestException exception = new RequestException(RequestException.CLIENT_PROTOL_EXCEPTION,
                "??");
        ret = ErrorUtil.errorJson("ERROR.HTTP.007", exception.getMessage());
        e.printStackTrace();
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpGet  request to url :" + url + "  ClientProtocolException " + e.getMessage());
    } catch (IOException e) {
        RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??");
        ret = ErrorUtil.errorJson("ERROR.HTTP.008", exception.getMessage());
        e.printStackTrace();
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpGet  request to url :" + url + "  IOException  " + e.getMessage());
    } finally {
        if (!Constants.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            LogUtil.d("result", ret);
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
        //request.//
        if (customLoadingDialog != null && customLoadingDialog.isShowing()) {
            customLoadingDialog.dismiss();
            customLoadingDialog = null;
        }
    }
    super.run();
}

From source file:cn.com.zhenshiyin.crowd.net.AsyncHttpPost.java

@Override
public void run() {
    String ret = "";
    try {/*from   w w  w.  j  a  v  a2  s  . c  om*/
        request = new HttpPost(url);
        if (Constants.isGzip) {
            request.addHeader("Accept-Encoding", "gzip");
        } else {
            request.addHeader("Accept-Encoding", "default");
        }
        LogUtil.d(AsyncHttpPost.class.getName(), "AsyncHttpPost  request to url :" + url);

        if (parameter != null && parameter.size() > 0) {
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            for (RequestParameter p : parameter) {
                list.add(new BasicNameValuePair(p.getName(), p.getValue()));
            }
            ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
        }
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            bis.mark(2);
            // ??
            byte[] header = new byte[2];
            int result = bis.read(header);
            // reset??
            bis.reset();
            // ?GZIP?
            int headerData = getShort(header);
            // Gzip ? ? 0x1f8b
            if (result != -1 && headerData == 0x1f8b) {
                LogUtil.d("HttpTask", " use GZIPInputStream  ");
                is = new GZIPInputStream(bis);
            } else {
                LogUtil.d("HttpTask", " not use GZIPInputStream");
                is = bis;
            }
            InputStreamReader reader = new InputStreamReader(is, "utf-8");
            char[] data = new char[100];
            int readSize;
            StringBuffer sb = new StringBuffer();
            while ((readSize = reader.read(data)) > 0) {
                sb.append(data, 0, readSize);
            }
            ret = sb.toString();
            bis.close();
            reader.close();

        } else {
            RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                    "??,??" + statusCode);
            ret = ErrorUtil.errorJson("ERROR.HTTP.001", exception.getMessage());
        }

        LogUtil.d(AsyncHttpPost.class.getName(), "AsyncHttpPost  request to url :" + url + "  finished !");
    } catch (java.lang.IllegalArgumentException e) {
        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                Constants.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("ERROR.HTTP.002", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (org.apache.http.conn.ConnectTimeoutException e) {
        RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION,
                Constants.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("ERROR.HTTP.003", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (java.net.SocketTimeoutException e) {
        RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION,
                Constants.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("ERROR.HTTP.004", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        RequestException exception = new RequestException(RequestException.UNSUPPORTED_ENCODEING_EXCEPTION,
                "?");
        ret = ErrorUtil.errorJson("ERROR.HTTP.005", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  UnsupportedEncodingException  " + e.getMessage());
    } catch (org.apache.http.conn.HttpHostConnectException e) {
        RequestException exception = new RequestException(RequestException.CONNECT_EXCEPTION, "");
        ret = ErrorUtil.errorJson("ERROR.HTTP.006", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  HttpHostConnectException  " + e.getMessage());
    } catch (ClientProtocolException e) {
        RequestException exception = new RequestException(RequestException.CLIENT_PROTOL_EXCEPTION,
                "??");
        ret = ErrorUtil.errorJson("ERROR.HTTP.007", exception.getMessage());
        e.printStackTrace();
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  ClientProtocolException " + e.getMessage());
    } catch (IOException e) {
        RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??");
        ret = ErrorUtil.errorJson("ERROR.HTTP.008", exception.getMessage());
        e.printStackTrace();
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  IOException  " + e.getMessage());
    } finally {
        if (!Constants.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            LogUtil.d("", ret);
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
        //
        if (customLoadingDialog != null && customLoadingDialog.isShowing()) {
            customLoadingDialog.dismiss();
            customLoadingDialog = null;
        }
    }
    super.run();
}

From source file:com.iloomo.net.AsyncHttpGet.java

@Override
public void run() {
    String ret = "";
    try {/*from www  .  jav  a  2s. c o  m*/
        if (parameter != null && parameter.size() > 0) {
            StringBuilder bulider = new StringBuilder();
            Set<String> keys = parameter.keySet();
            for (String key : keys) {
                if (bulider.length() != 0) {
                    bulider.append("&");
                }

                bulider.append(EncodeUtils.encode(key));
                bulider.append("=");
                bulider.append(EncodeUtils.encode(String.valueOf(parameter.get(key))));
            }
            url += "?" + bulider.toString();
            Log.i("request", url);
        }
        for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) {
            try {
                request = new HttpGet(url);
                if (HttpConstant.isGzip) {
                    request.addHeader("Accept-Encoding", "gzip");
                } else {
                    request.addHeader("Accept-Encoding", "default");
                }
                // 
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
                // ?
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    bis.mark(2);
                    // ??
                    byte[] header = new byte[2];
                    int result = bis.read(header);
                    // reset??
                    bis.reset();
                    // ?GZIP?
                    int headerData = getShort(header);
                    // Gzip ? ? 0x1f8b
                    if (result != -1 && headerData == 0x1f8b) {
                        is = new GZIPInputStream(bis);
                    } else {
                        is = bis;
                    }
                    InputStreamReader reader = new InputStreamReader(is, "utf-8");
                    char[] data = new char[100];
                    int readSize;
                    StringBuffer sb = new StringBuffer();
                    while ((readSize = reader.read(data)) > 0) {
                        sb.append(data, 0, readSize);
                    }
                    ret = sb.toString();
                    bis.close();
                    reader.close();

                    // ByteArrayOutputStream content = new
                    // ByteArrayOutputStream();
                    // response.getEntity().writeTo(content);
                    // ret = new String(content.toByteArray()).trim();
                    // content.close();
                } else {
                    ret = ErrorUtil.errorJson("" + statusCode, "??,??" + statusCode);
                    continue;
                }

                break;
            } catch (Exception e) {
                if (i == HttpConstant.CONNECTION_COUNT - 1) {
                    ret = ErrorUtil.errorJson("999", "");
                } else {
                    Log.d("connection url", "" + i);
                    continue;
                }
            }
        }

    } catch (IllegalArgumentException e) {

        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                HttpConstant.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("999", exception.getMessage());
    } finally {
        if (!HttpConstant.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
    }
    super.run();
}

From source file:com.changxiang.game.sdk.net.AsyncHttpGet.java

@Override
public void run() {
    String ret = "";
    try {//from  www .  java  2 s .  com
        if (parameter != null && parameter.size() > 0) {
            StringBuilder bulider = new StringBuilder();
            for (RequestParameter p : parameter) {
                if (bulider.length() != 0) {
                    bulider.append("&");
                }

                bulider.append(Util.encode(p.getName()));
                bulider.append("=");
                bulider.append(Util.encode(p.getValue()));
            }
            //            url += "?" + bulider.toString();
            url += "?" + bulider.toString() + "&sign="
                    + MD5Util.MD5(bulider.toString() + "1234" + CXGameConfig.SERVERKEY);

            System.out.println("===" + url);
        }
        LogUtil.d("AsyncHttpGet : ", url);
        for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) {
            try {
                request = new HttpGet(url);
                if (CXGameConfig.isGzip) {
                    request.addHeader("Accept-Encoding", "gzip");
                } else {
                    request.addHeader("Accept-Encoding", "default");
                }
                // 
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
                // ?
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    bis.mark(2);
                    // ??
                    byte[] header = new byte[2];
                    int result = bis.read(header);
                    // reset??
                    bis.reset();
                    // ?GZIP?
                    int headerData = getShort(header);
                    // Gzip ? ? 0x1f8b
                    if (result != -1 && headerData == 0x1f8b) {
                        is = new GZIPInputStream(bis);
                    } else {
                        is = bis;
                    }
                    InputStreamReader reader = new InputStreamReader(is, "utf-8");
                    char[] data = new char[100];
                    int readSize;
                    StringBuffer sb = new StringBuffer();
                    while ((readSize = reader.read(data)) > 0) {
                        sb.append(data, 0, readSize);
                    }
                    ret = sb.toString();
                    bis.close();
                    reader.close();

                } else {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "??,??" + statusCode);
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                    LogUtil.d("connection url", "" + i);
                    continue;
                }

                break;
            } catch (Exception e) {
                if (i == CXGameConfig.CONNECTION_COUNT - 1) {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "");
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                } else {
                    LogUtil.d("connection url", "" + i);
                    continue;
                }
            }
        }

    } catch (java.lang.IllegalArgumentException e) {

        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                CXGameConfig.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("-2", exception.getMessage());
    } finally {
        if (!CXGameConfig.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
        if (customLoadingDialog != null && customLoadingDialog.isShowing()) {
            customLoadingDialog.dismiss();
            customLoadingDialog = null;
        }
    }
    super.run();
}

From source file:org.jasig.portlet.newsreader.adapter.RomeAdapter.java

public void init() throws Exception {
    final HttpParams params = httpClient.getParams();
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
    params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, connectionManagerTimeout);

    httpClient.setHttpRequestRetryHandler(new RetryHandler());

    String proxyHost = null;/*  w ww .j av  a 2s  .  c  om*/
    String proxyPort = null;

    if (StringUtils.isBlank(this.proxyHost) && StringUtils.isBlank(this.proxyPort)) {
        log.trace("Checking for proxy configuration from system properties...");
        proxyHost = System.getProperty("http.proxyHost");
        proxyPort = System.getProperty("http.proxyPort");
        if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort)) {
            log.debug("Found proxy configuration from system properties");
        }
    } else {
        log.debug("Using proxy settings from fields set during bean construction");
        proxyHost = this.proxyHost;
        proxyPort = this.proxyPort;
    }

    if (!StringUtils.isBlank(proxyHost) && !StringUtils.isBlank(proxyPort)) {
        HttpHost proxy = new HttpHost(proxyHost, Integer.valueOf(proxyPort));
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        log.debug("Using proxy configuration to retrieve news feeds: " + proxyHost + ":" + proxyPort);
    } else {
        log.debug("No proxy configuration is set. Proceeding normally...");
    }

    // Spring configuration prevents us from using type AbstractHttpClient because we are wrapping the HTTP Client
    // with DecompressingHttpClient which only implements HttpClient, so sadly we're getting around it with a
    // second field.
    compressingClient = new DecompressingHttpClient(httpClient);
}