我们可以在Java中通过HttpURLConnection请求进行两次通信吗?

时间:2015-04-29 13:24:02

标签: java servlets httpurlconnection

我有客户端/服务器架构,我面临的问题是这样的。这里Client是java程序,使用HttpURLConnection连接,服务器是Servlet。客户端发出请求后,Servlet会通过Stream发送文件。并且客户端将从Stream下载它。这个过程很好。问题是假设如果文件未在客户端成功下载,则客户端必须向服务器发送消息,以便它再次发送文件(我希望在没有建立新连接的情况下发生这种情况)。如何在单个请求中实现此目的。任何人都可以帮助我。

3 个答案:

答案 0 :(得分:0)

为什么不写一个无限的while循环,这将继续连接到服务器,直到你得到肯定的响应。我的意思是

   Array (
[response] => Array (
[status] => Array (
[version] => 4.2
[code] => 0
[message] => Success
)
[songs] => Array (
[0] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOKGWES13D647BE466
[artist_name] => Kanye West
[title] => All Of The Lights
)
[1] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOHBKVU14509A9F6C3
[artist_name] => Kanye West
[title] => All Of The Lights
)
[2] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SODELAY13AD1ACC8CF
[artist_name] => Kanye West
[title] => All Of The Lights
)
[3] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOUDIYM14B7C7B2D95
[artist_name] => Kanye West
[title] => All of the Lights
)
[4] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOTEMPJ13DB921F71F
[artist_name] => Kanye West
[title] => All of the Lights (
Remix
)
)
[5] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOXIDRL13CCFBBC829
[artist_name] => Kanye West
[title] => All Of The Lights
[LbLuke Rmx]
)
[6] => Array (
[artist_id] => ARRH63Y1187FB47783
[id] => SOTJZSO12D857905F6
[artist_name] => Kanye West
[title] => All Of The Lights (
Interlude
)
)
[7] => Array (
[artist_id] => ARVCDGF12FE08689BA
[id] => SOGLUJD130516E0D00
[artist_name] => Made famous by Kanye West
[title] => All of the lights
)
)
)
)

答案 1 :(得分:0)

为了知道客户端有问题,您必须发送新请求。你不能做你想做的事。

答案 2 :(得分:0)

您的问题与此类似:How to Reuse HttpUrlConnection?

作为回报,已经得到了回答: Persistent HttpURLConnection in Java

您的问题的答案保持不变:您设置http.keepAlivehttp.maxConnections属性,让Java透明地处理HTTP持久性。

现在,我有理由相信这两个答案对您没有帮助,因为您通常对文件下载感兴趣,不是重新启动整个下载,而是继续之前的下载。

该问题也已在以下问题中得到解答:Java: resume Download in URLConnection

希望这有帮助!