Java Code Examples for org.apache.http.client.methods.HttpRequestWrapper#setURI()

The following examples show how to use org.apache.http.client.methods.HttpRequestWrapper#setURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedirectStrategy.java    From NetDiscovery with Apache License 2.0 7 votes vote down vote up
@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) {

            log.error("强转为HttpRequestWrapper出错");
        }
        return new HttpPost(uri);
    } else {
        return new HttpGet(uri);
    }
}
 
Example 2
Source File: AbstractHttpClientGenerator.java    From cetty with Apache License 2.0 6 votes vote down vote up
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
    URI uri = getLocationURI(request, response, context);
    String method = request.getRequestLine().getMethod();
    if (HttpConstants.POST.equalsIgnoreCase(method)) {
        try {
            HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request;
            httpRequestWrapper.setURI(uri);
            httpRequestWrapper.removeHeaders("Content-Length");
            return httpRequestWrapper;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HttpPost(uri);
    } else {
        return new HttpGet(uri);
    }
}
 
Example 3
Source File: CustomRedirectStrategy.java    From webmagic with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap);
}
 
Example 5
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, context);
}
 
Example 6
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request, target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap);
}
 
Example 7
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request, target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, context);
}
 
Example 8
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler);
}
 
Example 9
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler, context);
}
 
Example 10
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request,target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler);
}
 
Example 11
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request,target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler, context);
}
 
Example 12
Source File: BasicHttpSolrClientTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException,
IOException {
  log.info("Intercepted params: {}", context);

  HttpRequestWrapper wrapper = (HttpRequestWrapper) request;
  URIBuilder uribuilder = new URIBuilder(wrapper.getURI());
  uribuilder.addParameter("b", "\u4321");
  try {
    wrapper.setURI(uribuilder.build());
  } catch (URISyntaxException ex) {
    throw new HttpException("Invalid request URI", ex);
  }
}
 
Example 13
Source File: SeimiRedirectStrategy.java    From SeimiCrawler with Apache License 2.0 5 votes vote down vote up
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
    URI uri = getLocationURI(request, response, context);
    String method = request.getRequestLine().getMethod();
    if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)&& request instanceof HttpRequestWrapper) {
        HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request;
        httpRequestWrapper.setURI(uri);
        httpRequestWrapper.removeHeaders("Content-Length");
        return httpRequestWrapper;
    } else {
        return super.getRedirect(request,response,context);
    }
}
 
Example 14
Source File: AgpClient.java    From geoportal-server-harvester with Apache License 2.0 4 votes vote down vote up
private String execute(HttpUriRequest req, Integer redirectDepth) throws IOException {
  // Determine if we've reached the limit of redirection attempts
  if (redirectDepth > this.maxRedirects) {
    throw new HttpResponseException(HttpStatus.SC_GONE, "Too many redirects, aborting");
  }

  try (CloseableHttpResponse httpResponse = httpClient.execute(req); InputStream contentStream = httpResponse.getEntity().getContent();) {
    if (httpResponse.getStatusLine().getStatusCode()>=400) {
      throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase());
    } else if (httpResponse.getStatusLine().getStatusCode() >= 300) {
      // See if we can redirect the command
      Header locationHeader = httpResponse.getFirstHeader("Location");
      if (locationHeader != null) {
        try {
          HttpRequestWrapper newReq = HttpRequestWrapper.wrap(req);

          // Determine if this is a relataive redirection
          URI redirUrl = new URI(locationHeader.getValue());
          if (!redirUrl.isAbsolute()) {
            HttpHost target = URIUtils.extractHost(newReq.getURI());

            redirUrl = URI.create(
              String.format(
                "%s://%s%s",
                target.getSchemeName(),
                target.toHostString(),
                locationHeader.getValue()
              )
            );
          }
          
          newReq.setURI(redirUrl);

          return execute(newReq, ++redirectDepth);
        } catch (IOException | URISyntaxException e) {
          LOG.debug("Error executing request", e);
          throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase());
        }
      }
    }
    return IOUtils.toString(contentStream, "UTF-8");
  }
}