在这个小问题上,我几乎已经把头发撕了好几天了。希望这里的某个人可以发现我的错误(因为我确定它在我的最后)并且给我一些关于如何补救它的要点。如果我的解释失败,请告诉我,我会尝试以更简洁的方式重写它。
-
一些背景知识:作为小型项目的一部分,我一直在编写一个Java应用程序来监控浏览器中游戏发送的聊天和事件数据(供参考; Command & Conquer: Tiberium Alliances) 。遗憾的是,关于他们的Web服务如何工作的文档并不多,但是我设法将一系列请求(基于观察网络流量和其他开发人员发布的代码示例)拼凑起来处理登录等等。
一个特定的请求绝对拒绝完成,并且Java在暂停30秒后抛出连接重置异常。以下是此特定请求的连接日志。
2014/03/25 11:17:16:703 EDT [DEBUG] RequestAddCookies - CookieSpec selected: best-match
2014/03/25 11:17:16:703 EDT [DEBUG] RequestAuthCache - Auth cache not set in the context
2014/03/25 11:17:16:703 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: {s}->https://gamecdnorigin.alliances.commandandconquer.com:443][total kept alive: 1; route allocated: 0 of 2; total allocated: 1 of 20]
2014/03/25 11:17:16:703 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 2][route: {s}->https://gamecdnorigin.alliances.commandandconquer.com:443][total kept alive: 1; route allocated: 1 of 2; total allocated: 2 of 20]
2014/03/25 11:17:16:703 EDT [DEBUG] MainClientExec - Opening connection {s}->https://gamecdnorigin.alliances.commandandconquer.com:443
2014/03/25 11:17:16:755 EDT [DEBUG] HttpClientConnectionOperator - Connecting to gamecdnorigin.alliances.commandandconquer.com/212.100.228.211:443
*snip: 30 seconds of junk output*
2014/03/25 11:18:16:992 EDT [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-2: Shutdown connection
2014/03/25 11:18:16:992 EDT [DEBUG] MainClientExec - Connection discarded
2014/03/25 11:18:16:993 EDT [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-2: Close connection
2014/03/25 11:18:16:993 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 2][route: {s}->https://gamecdnorigin.alliances.commandandconquer.com:443][total kept alive: 1; route allocated: 0 of 2; total allocated: 1 of 20]
2014/03/25 11:18:16:993 EDT [INFO] RetryExec - I/O exception (java.net.SocketException) caught when processing request to {s}->https://gamecdnorigin.alliances.commandandconquer.com:443: Connection reset
2014/03/25 11:18:16:993 EDT [DEBUG] RetryExec - Connection reset <java.net.SocketException: Connection reset>java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:275)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:117)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:314)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
*snip: lots of extra backtrace*
我知道很多:/
奇怪的是,使用PHP运行完全相同的请求cURL工作正常,我收到了我期望的信息。下面是发送失败请求的Java的代码片段,以及成功完成的PHP。
爪哇:
public static boolean loadWorlds(Session session) {
HttpPost post = new HttpPost("https://gamecdnorigin.alliances.commandandconquer.com/Farm/Service.svc/ajaxEndpoint/GetOriginAccountInfo");
HttpEntity entity = new StringEntity(String.format("{\"session\":\"%s\"}", session.getId()), ContentType.APPLICATION_JSON);
post.setHeader("Content-Type", "application/json; charset=utf-8");
post.setHeader("X-Qooxdoo-Response-Type", "application/json");
post.setHeader("Cache-Control", "no-cache");
post.setHeader("Pragma", "no-cache");
post.setEntity(entity);
try {
CloseableHttpResponse resp = ApiCommunicator.getHttpClient().execute(post);
System.out.println(EntityUtils.toString(resp.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
通过PHP cURL:
<?php
$cookie = 'cookies/' . md5($user) . '.txt';
$data = array(
'session' => 'hidden :P'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://gamecdnorigin.alliances.commandandconquer.com/Farm/Service.svc/ajaxEndpoint/GetOriginAccountInfo');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json; charset=utf-8",
"Cache-Control: no-cache",
"Pragma: no-cache",
"X-Qooxdoo-Response-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$result = curl_exec($ch);
?>
为了尽可能缩短这篇文章的篇幅(因为它已经是一段痛苦的长篇文章)我稍微重写了PHP以显示$ cookie和$ data的值,因为两者都进一步设置在脚本的执行中。
任何帮助都将不胜感激。我不知道HttpComponents是否会向请求添加或删除标题数据(我刚刚开始在2天前开始学习该库,当我拿起这个项目时)cURL不会。如果有必要,我可以发布更多的代码示例,因为这个项目中没有任何内容真的是秘密&#34;除了我从服务器返回的个人登录/会话信息之外。另外,这是我第一次来这里D:所以,如果我没有正确格式化或者我的代码片段没有突出显示,请告诉我,我会尽我所能来解决它:)
答案 0 :(得分:0)
尝试通过关闭您获得的Closable实例来关闭底层连接。
CloseableHttpResponse resp = ApiCommunicator.getHttpClient().execute(post);
try {
System.out.println(EntityUtils.toString(resp.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
finally{
resp.close();
}
答案 1 :(得分:0)
我建议使用命令行选项-Djavax.net.debug = ssl运行您的Java客户端,因为这将为您提供更多(但更神秘)的SSL握手洞察。这应该说明底层连接问题,但可能需要一段时间才能理解此调试日志将输出的详细信息。 http://www.smartjava.org/content/how-analyze-java-ssl-errors提供了关于SSL握手的非常好的详细讨论。
您的HTTPClient没有明确定义的SSLContext似乎是合理的。您可以探索使用其中一些选项来创建它。
SSLContext sslContext = SSLContexts.createDefault();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
请注意,您还可以从SSLContexts.custom()创建自定义SSLContext,它允许您添加密钥库,信任库以及使用TLS指定。