Example usage for org.apache.http.protocol ExecutionContext HTTP_REQUEST

List of usage examples for org.apache.http.protocol ExecutionContext HTTP_REQUEST

Introduction

In this page you can find the example usage for org.apache.http.protocol ExecutionContext HTTP_REQUEST.

Prototype

String HTTP_REQUEST

To view the source code for org.apache.http.protocol ExecutionContext HTTP_REQUEST.

Click Source Link

Usage

From source file:yangqi.hc.HttpContextTest.java

/**
 * @param args/*from w  w  w  .  j  ava2s.  co m*/
 * @throws IOException
 * @throws ClientProtocolException
 */
public static void main(String[] args) throws ClientProtocolException, IOException {
    // TODO Auto-generated method stub
    HttpClient httpclient = new DefaultHttpClient();

    HttpContext context = new BasicHttpContext();

    // Prepare a request object
    HttpGet httpget = new HttpGet("http://www.apache.org/");

    // Execute the request
    HttpResponse response = httpclient.execute(httpget, context);

    showAttr(ExecutionContext.HTTP_CONNECTION, context);
    showAttr(ExecutionContext.HTTP_PROXY_HOST, context);
    showAttr(ExecutionContext.HTTP_REQ_SENT, context);
    showAttr(ExecutionContext.HTTP_RESPONSE, context);
    showAttr(ExecutionContext.HTTP_REQUEST, context);
    showAttr(ExecutionContext.HTTP_TARGET_HOST, context);

}

From source file:wuit.common.crawler.WebSit.Crawler.java

public static String doGetHttp(DSCrawlerUrl pageUrl) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 12000);
    HttpConnectionParams.setSoTimeout(params, 9000);
    HttpClient httpclient = new DefaultHttpClient(params);
    String rs = "";
    try {//from   ww  w  . j a v a  2 s.c o  m
        HttpGet httpget = new HttpGet(pageUrl.url);
        //            System.out.println("executing request " + pageUrl.url);
        HttpContext httpContext = new BasicHttpContext();
        //            httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
        httpget.addHeader("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");

        HttpResponse response = httpclient.execute(httpget, httpContext);
        HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        pageUrl.url = targetHost.toString() + realRequest.getURI();
        int resStatu = response.getStatusLine().getStatusCode();//? 
        if (resStatu == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                rs = EntityUtils.toString(entity, "iso-8859-1");
                String in_code = getEncoding(rs);
                String encode = getHtmlEncode(rs);
                if (encode.isEmpty()) {
                    httpclient.getConnectionManager().shutdown();
                    return "";
                } else {
                    if (!in_code.toLowerCase().equals("utf-8")
                            && !in_code.toLowerCase().equals(encode.toLowerCase())) {
                        if (!in_code.toLowerCase().equals("iso-8859-1"))
                            rs = new String(rs.getBytes("iso-8859-1"), in_code);
                        if (!encode.toLowerCase().equals(in_code.toLowerCase()))
                            rs = new String(rs.getBytes(in_code), encode);
                    }
                }
                try {
                } catch (RuntimeException ex) {
                    httpget.abort();
                    throw ex;
                } finally {
                    // Closing the input stream will trigger connection release
                    //                    try { instream.close(); } catch (Exception ignore) {}
                }
            }
        }
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
        return rs;
    }
}

From source file:code.google.restclient.core.CustomRetryHandler.java

public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {

    HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);

    // idempotent are those methods which returns same value irrespective of number of execution

    boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
    if (!idempotent) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): not retrying because request is non-idempotent");
        }/*from   w  ww .  jav a  2  s. c  om*/
        // Retry if the request is considered idempotent
        return false;
    }

    if (executionCount >= 5) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): not retrying because execution count exceeds 5");
        }
        // Do not retry if over max retry count
        return false;
    }

    if (exception instanceof NoHttpResponseException) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): retrying because server dropped connection");
        }
        // Retry if the server dropped connection on us
        return true;
    }

    if (exception instanceof SSLHandshakeException) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): not retrying because of SSL handshake exception");
        }
        // Do not retry on SSL handshake exception
        return false;
    }

    if (idempotent) {
        if (DEBUG_ENABLED) {
            LOG.debug("retryRequest(): retrying because request is idempotent");
        }
        // Retry if the request is considered idempotent
        return true;
    }

    return false;
}

From source file:wuit.common.crawler.search.Crawler.java

public static String doGetHttp(String url) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 12000);
    HttpConnectionParams.setSoTimeout(params, 9000);
    HttpClient httpclient = new DefaultHttpClient(params);
    String rs = "";
    try {//from  w  ww .ja  v a2s  .co  m
        //            System.out.println(url);
        HttpGet httpget = new HttpGet(url);

        HttpContext httpContext = new BasicHttpContext();
        //            httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
        httpget.addHeader("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");

        HttpResponse response = httpclient.execute(httpget, httpContext);
        HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        url = targetHost.toString() + realRequest.getURI();
        int resStatu = response.getStatusLine().getStatusCode();//? 
        if (resStatu == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                rs = EntityUtils.toString(entity, "iso-8859-1");
                String in_code = getEncoding(rs);
                String encode = getHtmlEncode(rs);
                if (encode.isEmpty()) {
                    httpclient.getConnectionManager().shutdown();
                    return "";
                } else {
                    if (!in_code.toLowerCase().equals("utf-8")
                            && !in_code.toLowerCase().equals(encode.toLowerCase())) {
                        if (!in_code.toLowerCase().equals("iso-8859-1"))
                            rs = new String(rs.getBytes("iso-8859-1"), in_code);
                        if (!encode.toLowerCase().equals(in_code.toLowerCase()))
                            rs = new String(rs.getBytes(in_code), encode);
                    }
                }
                try {
                } catch (RuntimeException ex) {
                    httpget.abort();
                    throw ex;
                } finally {
                    // Closing the input stream will trigger connection release
                    //                    try { instream.close(); } catch (Exception ignore) {}
                }
            }
        }
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
        return rs;
    }
}

From source file:ResponseGzipCompress.java

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }/* w  w w .ja  v a  2  s. co  m*/
    HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    Header aeheader = request.getFirstHeader(ACCEPT_ENCODING);
    if (aeheader != null) {
        HeaderElement[] codecs = aeheader.getElements();
        for (int i = 0; i < codecs.length; i++) {
            if (codecs[i].getName().equalsIgnoreCase(GZIP_CODEC) && response.getEntity() != null) {
                response.setEntity(new NGzipCompressingEntity(response.getEntity()));
                return;
            }
        }
    }
}

From source file:z.hol.net.http.entity.compress.ResponseGzipCompress.java

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }/*from   ww  w  .j a v  a2 s . c o m*/
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        Header aeheader = request.getFirstHeader(ACCEPT_ENCODING);
        if (aeheader != null) {
            HeaderElement[] codecs = aeheader.getElements();
            for (int i = 0; i < codecs.length; i++) {
                if (codecs[i].getName().equalsIgnoreCase(GZIP_CODEC)) {
                    response.setEntity(new GzipCompressingEntity(entity));
                    return;
                }
            }
        }
    }
}

From source file:org.ocpsoft.redoculous.tests.HttpAction.java

/**
 * Return the current full URL.//from   ww w. j  a  v  a 2s.co m
 */
public String getCurrentURL() {
    HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    String currentUrl = currentHost.toURI() + currentReq.getURI();

    if (currentUrl.startsWith(baseUrl)) {
        currentUrl = currentUrl.substring(baseUrl.length());
    }

    return currentUrl;
}

From source file:org.apache.axis2.transport.http.server.AxisHttpRequestImpl.java

public void prepare() throws IOException, HttpException {
    this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, this.conn);
    this.context.setAttribute(ExecutionContext.HTTP_REQUEST, this.request);

    this.httpproc.process(this.request, this.context);
}

From source file:com.hippoapp.asyncmvp.http.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > DEFAULT_MAX_RETRIES) {
        retry = false;//from   w  w  w.j a v a  2 s.  c o  m
    } else if (sUnretriedExceptionSet.contains(exception.getClass())) {
        retry = false;
    } else if (sRetriedExceptionSet.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully
        // sent yet
        retry = true;
    } else {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String requestType = currentReq.getMethod();
        if (!requestType.equals("POST")) {
            retry = true;
        } else {
            // otherwise do not retry
            retry = false;
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_IN_MILLS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}

From source file:com.netflix.http4.NFHttpMethodRetryHandler.java

@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ICAST_INTEGER_MULTIPLY_CAST_TO_LONG")
public boolean retryRequest(final IOException exception, int executionCount, HttpContext context) {
    if (super.retryRequest(exception, executionCount, context)) {
        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String methodName = request.getRequestLine().getMethod();
        String path = "UNKNOWN_PATH";
        if (request instanceof HttpUriRequest) {
            HttpUriRequest uriReq = (HttpUriRequest) request;
            path = uriReq.getURI().toString();
        }//  w  w  w.j a v  a  2  s.  c om
        try {
            Thread.sleep(executionCount * this.sleepTimeFactorMs);
        } catch (InterruptedException e) {
            logger.warn("Interrupted while sleep before retrying http method " + methodName + " " + path, e);
        }
        DynamicCounter.increment(RETRY_COUNTER + methodName + ":" + path);
        return true;
    }
    return false;
}