我在httpclient 4.1的默认httpParams上找不到任何文档?
我做GET时默认的套接字超时是多少?
我已经使用aws java API工作了一段时间而没有那么多问题.目前我正在使用1.5.2版本的库.
当我使用以下代码迭代文件夹中的对象时:
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(MyClass.class.getResourceAsStream("AwsCredentials.properties")));
String s3Key = "folder1/folder2";
String bucketName = Constantes.S3_BUCKET;
String key = s3Key +"/input_chopped/";
ObjectListing current = s3.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix(key));
boolean siguiente = true;
while (siguiente) {
siguiente &= current.isTruncated();
contador += current.getObjectSummaries().size();
for (S3ObjectSummary objectSummary : current.getObjectSummaries()) {
S3Object object = s3.getObject(new GetObjectRequest(bucketName, objectSummary.getKey()));
System.out.println(object.getKey());
}
current=s3.listNextBatchOfObjects(current);
}
Run Code Online (Sandbox Code Playgroud)
要点:链接:https://gist.github.com/fgblanch/6038699 我收到以下异常:
INFO (AmazonHttpClient.java:358) - Unable to execute HTTP request: Timeout waiting for connection from pool
org.apache.http.conn.ConnectionPoolTimeoutException: Timeout …Run Code Online (Sandbox Code Playgroud) 我目前正在使用OAuth-Signpost Java库来签署从客户端发送到实现OAuth身份验证的服务器的请求.在进行GET请求时(使用HttpURLConnection)一切正常:请求被签名,参数被包含,签名在目的地中匹配.但是,它似乎不适用于POST请求.我知道使用HttpURLConnection签名POST时可能出现的问题,因此我转移到Apache HttpComponents库以获取这些请求.我在以下示例中发送的参数是纯字符串和类似XML的字符串('rxml').我的代码如下:
public Response exampleMethod(String user, String sp, String ep, String rn, String rxml){
//All these variables are proved to be correct (they work right in GET requests)
String uri = "...";
String consumerKey = "...";
String consumerSecret = "...";
String token = "...";
String secret = "...";
//create the parameters list
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", user));
params.add(new BasicNameValuePair("sp", sp));
params.add(new BasicNameValuePair("ep", ep));
params.add(new BasicNameValuePair("rn", rn));
params.add(new BasicNameValuePair("rxml", rxml));
// create a consumer object and configure …Run Code Online (Sandbox Code Playgroud) 我正在尝试向受IAM访问策略保护的AWS Elasticsearch域发出HTTP请求.我需要签署这些请求,以便AWS授权这些请求.我正在使用Jest,后者又使用Apache HttpComponents Client.
这似乎是一个常见的用例,我想知道是否有某种类型的库,我可以在Apache HttpComponents客户端上使用它来签署所有请求.
java amazon-web-services elasticsearch apache-httpcomponents jest
有人可以指导我如何配置HttpComponentsClientHttpRequestFactory使用代理服务器.
我见过的所有例子都在使用SimpleClientHttpRequestFactory.
如何使用Apache HttpComponents HttpClient 5.1绕过证书验证错误?
我找到了一个可行的解决方案来绕过 HttpClient 4.5 中的此类错误,建议自定义HttpClient实例:
HttpClient httpClient = HttpClients
.custom()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
Run Code Online (Sandbox Code Playgroud)
但它不适用于 HttpClient 5.1,因为(which returns)中不存在setSSLContext和方法。setSSLHostnameVerifierHttpClientBuilderHttpClients.custom()
我一直在使用java处理Google OAuth 2.0,并在实现过程中遇到了一些未知错误.
以下用于POST请求的CURL工作正常:
curl -v -k --header "Content-Type: application/x-www-form-urlencoded" --data "code=4%2FnKVGy9V3LfVJF7gRwkuhS3jbte-5.Arzr67Ksf-cSgrKXntQAax0iz1cDegI&client_id=[my_client_id]&client_secret=[my_client_secret]&redirect_uri=[my_redirect_uri]&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
Run Code Online (Sandbox Code Playgroud)
并产生所需的结果.
但是以下在java中执行上面的POST请求会导致一些错误和响应"invalid_request"
请检查以下代码并指出这里出错:(使用Apache http组件)
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
HttpParams params = new BasicHttpParams();
params.setParameter("code", code);
params.setParameter("client_id", client_id);
params.setParameter("client_secret", client_secret);
params.setParameter("redirect_uri", redirect_uri);
params.setParameter("grant_type", grant_type);
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
post.setParams(params);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(post);
Run Code Online (Sandbox Code Playgroud)
试过URLEncoder.encode( param , "UTF-8")每个参数,但这也不起作用.
可能是什么原因?
我一直在查看摘要认证示例:
http://hc.apache.org/httpcomponents-client-4.3.x/examples.html
在我的场景中,有几个线程发出HTTP请求,并且每个线程都必须使用自己的一组凭据进行身份验证.另外,请考虑这个问题可能非常具体针对Apache HTTP客户端4.3以上,4.2可能以不同的方式处理身份验证,尽管我自己没有检查它.也就是说,实际问题就出现了.
我想只使用一个客户端实例(该类的静态成员,即线程安全)并为其提供连接管理器以支持多个并发请求.关键是每个请求都会提供不同的凭据,我没有看到为每个请求分配凭据的方法,因为在构建http客户端时设置了凭据提供程序.从上面的链接:
[...]
HttpHost targetHost = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
Run Code Online (Sandbox Code Playgroud)
[...]
检查:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d5e600
第4.4点(寻求4.4.HTTP认证和执行上下文)中的代码示例似乎表示HttpClientContext被赋予auth高速缓存和凭证提供者,然后被传递给HTTP请求.在它旁边执行请求,似乎客户端将在HTTP请求中获得主机的凭据过滤.换句话说:如果上下文(或缓存)具有当前HTTP请求的目标主机的有效凭据,则他将使用它们.对我来说问题是不同的线程将对同一主机执行不同的请求.
有没有办法为每个HTTP请求提供自定义凭据?
在此先感谢您的时间!:)
http credentials digest apache-httpcomponents apache-httpclient-4.x
我正在尝试在 Spring 中设置 httpClient5 ...我有以下代码:
PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setTlsVersions(TLS.V_1_3, TLS.V_1_2)
.build())
.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(Timeout.ofSeconds(5))
.build())
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
.setConnPoolPolicy(PoolReusePolicy.LIFO)
.setConnectionTimeToLive(TimeValue.ofMinutes(1L))
.build();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(Timeout.ofSeconds(5))
.setResponseTimeout(Timeout.ofSeconds(5))
.setCookieSpec(StandardCookieSpec.STRICT)
.build())
.build();
CookieStore cookieStore = new BasicCookieStore();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
HttpClientContext clientContext = HttpClientContext.create();
clientContext.setCookieStore(cookieStore);
clientContext.setCredentialsProvider(credentialsProvider);
clientContext.setRequestConfig(RequestConfig.custom()
.setConnectTimeout(Timeout.ofSeconds(5000))
.setResponseTimeout(Timeout.ofSeconds(5000))
.build());
// connect Spring httpComponent (client-side) with Apache httpClient
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(60000);
httpRequestFactory.setConnectTimeout(60000);
httpRequestFactory.setHttpClient(client);
Run Code Online (Sandbox Code Playgroud)
ClosableHttpClient 类实现了与 setHttpClient(client) 不兼容的接口 org.apache.hc.client5.http.classic.HttpClient ;这是运行时错误:
java.lang.ClassCastException: class org.apache.hc.client5.http.impl.classic.InternalHttpClient cannot be cast …Run Code Online (Sandbox Code Playgroud) 我正在迁移我的应用程序以使用 HttpClient5,但它一天比一天变得痛苦。许多 API 被删除,并且没有适当的文档可用于了解替代方案。Stackoverflow/任何其他网站/博客总是显示与 httpcomponents4.x 相关的答案,但这些 API 不再存在于 HttpClient5 中。我将所有查询/替代方案放在这里。如果有人知道,请回答/确认这些是否是正确的实现。
在客户端上设置套接字超时:删除了RequestConfig.custom().setSocketTimeout(socketTimeout).build()API。经过大量研究,发现有单独的 SocketConfig 类需要设置ConnectionManager
SocketConfig socketConfig=SocketConfig.custom()
.setSoTimeout(Timeout.ofMilliseconds(10000))
.build();
BasicHttpClientConnectionManager connMgr=new BasicHttpClientConnectionManager(registry);
connMgr.setSocketConfig(socketConfig);
CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(connMgr)
.build();
Run Code Online (Sandbox Code Playgroud)
设置默认主机。遵循此How does one set Default HttpHost Target in Apache HttpClient 4.3+? 并发现我们可以重写determineRoute,DefaultRoutePlanner但我们不能在 httpclient5 中执行此操作,因为此方法声明为final因此无法重写。所以我这样做了:
HttpHost targetHost = new HttpHost(myHost,myPort);
HttpRoutePlanner planner=new HttpRoutePlanner() {
@Override
public HttpRoute determineRoute(HttpHost var1, HttpContext var2) throws HttpException {
HttpRoute route=new HttpRoute(targetHost);//default for all requests
return route; …Run Code Online (Sandbox Code Playgroud)apache-httpcomponents apache-commons-httpclient apache-httpclient-4.x apache-httpclient-5.x
java ×7
spring ×2
amazon-s3 ×1
credentials ×1
digest ×1
google-oauth ×1
http ×1
http-post ×1
jest ×1
oauth ×1
post ×1
resttemplate ×1
signpost ×1
ssl ×1