gpt4 book ai didi

java - cURL 有效,但 Apache 的 HttpComponents 库中的相同请求不起作用?

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

为了这个小问题,我几天来一直在焦头烂额。希望这里有人可以发现我的错误(因为我确定这是我的错误)并给我一些关于如何补救它的要点。如果我的解释失败,请告诉我,我会尝试以更简洁的方式重写它。

--

一些背景知识:作为一个小型副项目的一部分,我一直在编写一个 Java 应用程序来监视浏览器内游戏发送的聊天和事件数据(供引用;Command & Conquer: Tiberium Alliances)。可悲的是,关于他们的网络服务如何工作的文档并不多,但我设法拼凑了一系列请求(基于观察网络流量和其他开发人员发布的代码示例)来处理登录等。

一个特定的请求绝对拒绝完成,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 的代码片段。

Java:

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 是否向请求添加或删除 header 数据(我确实是在 2 天前开始学习这个库的,当时我选择了这个项目)而 cURL 不会。如有必要,我可以发布更多代码示例,因为除了从服务器返回的我的个人登录/ session 信息之类的东西之外,这个项目中没有什么是真正“ secret ”的。另外,这是我第一次来这里 D: 所以如果我的格式不正确或者我的代码片段没有正确突出显示,请告诉我,我会尽力修复它:)

最佳答案

尝试通过关闭您获得的 Closable 实例来关闭底层连接。

CloseableHttpResponse resp = ApiCommunicator.getHttpClient().execute(post);
try {
System.out.println(EntityUtils.toString(resp.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
finally{
resp.close();
}

关于java - cURL 有效,但 Apache 的 HttpComponents 库中的相同请求不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22639828/

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