HttpClient是一个用于浏览网站的Java库.
我想将它与Kerberos一起使用.HttpClient文档的Kerberos部分主要说:
最好的方法是在示例中获取KerberosHttpClient.java文件并尝试使其工作.
然而,KerberosHttpClient.java无处可寻(源,二进制文件,doc)
没有这个文件很难做任何事情.
我在哪里可以找到KerberosHttpClient.java?
或者是否有更好的文档解释如何将HttpClient与Kerberos一起使用?
我使用apache常见的httpclient 4.3.3来发出http 1.0请求.以下是我提出请求的方式
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setProtocolVersion(new ProtocolVersion("HTTP", 1, 0));
// trying to remove default headers but it doesn't work
post.removeHeaders("User-Agent");
post.removeHeaders("Accept-Encoding");
post.removeHeaders("Connection");
post.setEntity(new ByteArrayEntity(ba) );
HttpResponse response = client.execute(post);
Run Code Online (Sandbox Code Playgroud)
但是,我可以看到有其他标头自动添加到我的服务器请求中
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.3 (java 1.5)
Accept-Encoding: gzip,deflate
Run Code Online (Sandbox Code Playgroud)
如何告诉httpclient不要包含任何其他标题?我试图用post.removeHeaders(xxxx)删除那些标题,但它不起作用.你能告诉我怎么样吗?
谢谢,
我非常频繁地(> = 1 /秒)对API端点进行HTTP POST,我想确保我有效地进行.我的目标是尽快成功或失败,特别是因为我有单独的代码来重试失败的POST.有一个很好的HttpClient性能提示页面,但我不确定是否详尽地实现它们都会带来真正的好处.这是我现在的代码:
public class Poster {
private String url;
// re-use our request
private HttpClient client;
// re-use our method
private PostMethod method;
public Poster(String url) {
this.url = url;
// Set up the request for reuse.
HttpClientParams clientParams = new HttpClientParams();
clientParams.setSoTimeout(1000); // 1 second timeout.
this.client = new HttpClient(clientParams);
// don't check for stale connections, since we want to be as fast as possible?
// this.client.getParams().setParameter("http.connection.stalecheck", false);
this.method = new PostMethod(this.url);
// custom …Run Code Online (Sandbox Code Playgroud) 如果我想要处理此网址,例如:
post = new HttpPost("http://testurl.com/lists/lprocess?action=LoadList|401814|1");
Run Code Online (Sandbox Code Playgroud)
Java/Apache不会让我,因为它说垂直条("|")是非法的.
用双斜线转义它也不起作用:
post = new HttpPost("http://testurl.com/lists/lprocess?action=LoadList\\|401814\\|1");
Run Code Online (Sandbox Code Playgroud)
^不起作用.
有任何建议如何使这项工作?
我试图接受所有证书,和/或使用Apache HTTPClient 4.5版接受自签名证书(这里的教程链接)
我已经从SO上的一堆帖子中解决了这个问题.到目前为止,他们都没有工作过.
我一直收到这个错误: Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
Apache文档:
相关的StackOverflow问题 - 以下是我尝试过的解决方案的一些链接:
请注意,在所有这些示例中,我还传递了我之前定义的cookie存储和代理凭据提供程序.这些都在工作,我只是想添加SSL支持.
使用创建我自己的ssl上下文SSLContextBuilder并信任所有自签名策略TrustSelfSignedStrategy.
SSLContextBuilder sshbuilder = new SSLContextBuilder();
sshbuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sshbuilder.build());
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.setSSLSocketFactory(sslsf)
.build();
Run Code Online (Sandbox Code Playgroud)
结果:没有用.拿到Error while trying to …
java ssl httpclient apache-commons-httpclient apache-httpclient-4.x
我正在迁移我的应用程序以使用 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
我设法对以下所有内容进行了更改:
HttpClient client;
HttpPost method;
client = new DefaultHttpClient();
method = new HttpPost(url);
InputStream rstream;
try {
rstream = method.getResponseBodyAsStream();
} catch (IOException e) {
return BadSpot(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
我不确定的是我应该替换getResponseBodyAsStream().
我正在使用Jakarta Commons HttpClient 3.1编写一个负载测试工具,该工具需要针对不同的服务器,并假装它针对HTTP服务器中的正确虚拟主机.为此,我需要能够将请求中的"主机"HTTP标头设置为不同的主机名,然后是我要连接的实际主机名.
看起来很明显我应该使用Method.setRequestHeader("Host","fakehostname"),但HttpClient只是忽略了这一点,并且总是在"Host"标题中发送我正在连接的真实主机名(我已启用"httpclient.wire"的调试日志记录,我可以这样做具体而言).
如何覆盖标题以便HttpClient注意?
如何从HttpClient类型的现有对象获取cookie?我正在使用HttpClient版本4.3.3,它没有方法httpClient.getCookieStore()了.
我正在将应用升级到org.apache.http不推荐使用的API 23 .
我当前(已弃用)的代码如下所示:
HttpClient httpClient = new DefaultHttpClient();
File file = new File(attr.Value);
String url = server_url;
HttpPost request = new HttpPost(url);
FileEntity fileEntity = new FileEntity(file, "image/png");
request.setEntity(fileEntity);
HttpResponse response = httpClient.execute(request);
String output = getContent(response.getEntity().getContent());
Run Code Online (Sandbox Code Playgroud)
我已经找到了一些关于如何使用它的建议HttpURLConnection,但它们比当前的解决方案(不能再使用)复杂得多.我正在谈论用于执行与上述相同功能的许多代码行.
有没有人有一个很好的固定更短的解决方案呢?
java android httpurlconnection apache-commons-httpclient android-6.0-marshmallow