我确定我错过了一些东西,但它告诉我,当涉及到参数时,HttpClient发送请求的行为会有所不同。
问题是,任何带参数的请求都会导致状态码501。 在4.2版本中,这些请求得到了妥善处理。
棘手的部分是,参数没有任何空间,当参数通过URIBuilder构建时问题也是原理,如下所述: http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/fundamentals.html
我想我需要一种方法来设置params:BasicHttpsParams集合而不是将它们与普通的uri连接 - 因为它们不会被HttpGet识别出来。此时4.2和4.3之间有什么变化吗?
这是我们的get方法实现的代码:
private static CloseableHttpClient httpAgent = initializeCloseableHttpClient(connectionManager);
private static CloseableHttpClient initializeCloseableHttpClient(PoolingHttpClientConnectionManager connectionManager) {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(500)
.setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.build();
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setCharset(StandardCharsets.UTF_8)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.setDefaultConnectionConfig(connectionConfig)
.build();
return httpClient;
}
public static String get(String url, Map<String, String> arguments) {
String argumentString = getArgumentString(arguments == null ? EMPTY_COLLECTION : arguments.entrySet());
HttpGet getMethod = new HttpGet(url + argumentString);
return request(getMethod, null);
}
private static String request(HttpUriRequest method, AuthenticationDetails authenticationDetails) {
InputStreamProcessor processor = new CopyToStringInputStreamProcessor();
processStream(method, authenticationDetails, processor);
return (String) processor.getResult();
}
private static void processStream(HttpUriRequest method, AuthenticationDetails authenticationDetails, InputStreamProcessor processor) {
try {
HttpClientContext context = null;
if (authenticationDetails != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(authenticationDetails.getUsername(), authenticationDetails.getPassword());
credsProvider.setCredentials(new AuthScope(method.getURI().getHost(), method.getURI().getPort()), credentials);
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
HttpHost targetHost = new HttpHost(method.getURI().getHost(), method.getURI().getPort(), method.getURI().getScheme());
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
}
for (int i = 0; i < 3; i++) {
CloseableHttpResponse response = httpAgent.execute(method, context);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200 && statusCode != 302) { // redirect is also ok
throw new HttpClientException(String.format("A http request responds with failure code: %d (%s), requested uri: %s", statusCode, response.getStatusLine().getReasonPhrase(), method.getRequestLine().getUri()));
}
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream responseStream = entity.getContent()) {
processor.process(responseStream);
EntityUtils.consume(entity);
}
catch (IOException ex) {
throw ex; // In case of an IOException the connection will be released back to the connection manager automatically
}
catch (RuntimeException ex) {
method.abort(); // In case of an unexpected exception you may want to abort the HTTP request in order to shut down the underlying connection and release it back to the connection manager.
throw ex;
}
}
} finally {
response.close();
}
}
}
catch (IOException e) {
throw new HttpClientException(String.format("IO exception while processing http request on url: %s. Message: %s", method.getRequestLine().getUri(), e.getMessage()), e);
}
catch (Exception e) {
throw new HttpClientException(String.format("Exception while processing http request on on url: %s. Message: %s", method.getRequestLine().getUri(), e.getMessage()), e);
}
}
高度赞赏任何可能出错的建议。
由于
答案 0 :(得分:0)
5xx是服务器错误。在您查看服务器日志之前,没有理由认为客户端出现任何问题。如果您无法访问服务器日志,请检查响应文本,可能会显示服务器上的失败信息。
答案 1 :(得分:0)
问题出现在以下循环中:
for (int i = 0; i < 3; i++) {...}
我不知道它何时进入代码,虽然它有理由为什么请求是3次,但发现它在删除之前和之后都不存在,一切都开始正常工作