下面列出了org.apache.http.client.methods.HttpRequestWrapper#setURI() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
@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);
}
}
@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);
}
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
}
@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);
}
}
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");
}
}