gpt4 book ai didi

java - HttpClient 4.3 更改了参数(查询)处理?

转载 作者:行者123 更新时间:2023-11-29 08:50:54 25 4
gpt4 key购买 nike

我确定我遗漏了一些东西,但在我看来,当涉及到参数时,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);
}
}

非常感谢任何可能出错的建议。

谢谢

最佳答案

5xx 是服务器错误。在查看服务器日志之前,没有理由假设客户端有任何问题。如果您无权访问服务器日志,请检查响应文本,那里可能会指示服务器上发生了什么故障。

关于java - HttpClient 4.3 更改了参数(查询)处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22758460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com