Search in sources :

Example 1 with HttpRequestWrapper

use of org.apache.http.client.methods.HttpRequestWrapper in project webmagic by code4craft.

the class CustomRedirectStrategy method getRedirect.

@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
    URI uri = getLocationURI(request, response, context);
    String method = request.getRequestLine().getMethod();
    if ("post".equalsIgnoreCase(method)) {
        try {
            HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request;
            httpRequestWrapper.setURI(uri);
            httpRequestWrapper.removeHeaders("Content-Length");
            return httpRequestWrapper;
        } catch (Exception e) {
            logger.error("强转为HttpRequestWrapper出错");
        }
        return new HttpPost(uri);
    } else {
        return new HttpGet(uri);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper) URI(java.net.URI) ProtocolException(org.apache.http.ProtocolException)

Example 2 with HttpRequestWrapper

use of org.apache.http.client.methods.HttpRequestWrapper in project metrics by dropwizard.

the class HttpClientMetricNameStrategiesTest method rewriteRequestURI.

private static HttpRequest rewriteRequestURI(HttpRequest request) throws URISyntaxException {
    HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
    URI uri = URIUtils.rewriteURI(wrapper.getURI(), null, true);
    wrapper.setURI(uri);
    return wrapper;
}
Also used : HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper) URI(java.net.URI)

Example 3 with HttpRequestWrapper

use of org.apache.http.client.methods.HttpRequestWrapper in project opennms by OpenNMS.

the class HttpClientWrapper method execute.

/**
     * Execute the given HTTP method, returning an HTTP response.
     * 
     * Note that when you are done with the response, you must call {@link #closeResponse()} so that it gets cleaned up properly.
     */
public CloseableHttpResponse execute(final HttpUriRequest method) throws ClientProtocolException, IOException {
    LOG.debug("execute: " + this.toString() + "; method: " + method.toString());
    // override some headers with our versions
    final HttpRequestWrapper requestWrapper = HttpRequestWrapper.wrap(method);
    if (!isEmpty(m_userAgent)) {
        requestWrapper.setHeader(HTTP.USER_AGENT, m_userAgent);
    }
    if (!isEmpty(m_virtualHost)) {
        requestWrapper.setHeader(HTTP.TARGET_HOST, m_virtualHost);
    }
    if (m_version != null) {
        if (HttpVersion.HTTP_1_1.equals(m_version) && isEmpty(m_virtualHost)) {
            // NMS-7506, set HTTP version to 1.0 if virtual host is not set (since 1.1 requires a virtual host)
            requestWrapper.setProtocolVersion(HttpVersion.HTTP_1_0);
        } else {
            requestWrapper.setProtocolVersion(m_version);
        }
    }
    return getClient().execute(requestWrapper);
}
Also used : HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper)

Aggregations

HttpRequestWrapper (org.apache.http.client.methods.HttpRequestWrapper)3 URI (java.net.URI)2 ProtocolException (org.apache.http.ProtocolException)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPost (org.apache.http.client.methods.HttpPost)1