[英]http-post through proxy not working
我可以使用chrome浏览器访问https://pushover.net/ ,但是当我尝试使用java到达同一个网站以连接到api并发送数据时,它将失败。
这是我的代码
HttpClient httpClient = (HttpClient) HttpClientBuilder.create()/*.setProxy(proxy)*/.build();
HttpPost post = new HttpPost("https://api.pushover.net/1/messages.json");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("token", apiToken));
nvps.add(new BasicNameValuePair("user", userKey));
nvps.add(new BasicNameValuePair("message", message));
post.setEntity(new UrlEncodedFormEntity(nvps, Charset.defaultCharset()));
//point A
HttpResponse deviceResponse = httpClient.execute(post);
//point B
它到达点A,但要花点时间才能到达点B,并且在到达时给出异常。 异常是org.apache.http.conn.HttpHostConnectException:连接到api.pushover.net:443(api.pushover.net/108.59.13.232]失败:连接计时器超时:连接。
我尝试将代理与下面的代码一起使用
HttpHost proxy = new HttpHost("url",9090,"https");
httpClient = (HttpClient) HttpClientBuilder.create().setProxy(proxy).build();
这给了我一个javax.net.ssl.SSLHandshakeException:握手期间远程主机关闭了连接
然后我也尝试添加
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
但它提供了相同的SSLHandshakeException。
我已经看到了创建密钥库的事情,但是那只能在我的机器上工作,我想要可以在代码中工作的东西,并允许我将该应用程序部署到1000台机器上,而不必对每个机器进行额外的手动配置。
有什么我应该做的更好的事情吗?
我在一开始用此代码代替了httpClient
修复了它。
HttpHost proxy = new HttpHost(PROXY_URL, 8080);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
AuthCache authCache = new BasicAuthCache();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(PROXY_URL, 8080, AuthScope.ANY_HOST, "ntlm"), new NTCredentials(authUser, authPassword, "",PROXY_DOMAIN));
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).setRoutePlanner(routePlanner).build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.