维护持久的http连接

发布于 2024-09-27 21:21:24 字数 125 浏览 8 评论 0原文

我的 Android 应用程序每 10 秒向 servlet 发送一次数据。 Servlet 接收第一个请求并做出响应。但 servlet 在接下来的 10 秒后没有收到客户端发送的第二组数据。有人可以告诉我我该怎么做吗?与会话有关吗?

My android app sends data to a servlet every 10 seconds. the servlet receives the very first request and responds back. but the servlet doesn't receive the second set of data that the client sends after the next 10 seconds. could some one please tel me how do i go about doing this. is it something related to session?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-10-04 21:21:24

HTTP 不是持久连接协议。您应该考虑为需要发送的每组数据发出一个 http 请求。

如果持久连接是强制性的(但我​​真的不知道是什么会迫使你这样做),你必须使用 TCP 协议......并且你将无法在服务器端使用 servlet而是侦听特定 TCP 端口的特定应用程序。

Http is not a persistent connection protocol. You should consider issuing one http request for each set of data you need to send.

If a persistent connection is mandatory (but I don't really see what would force you to do that), you have to work with the TCP protocol... and you won't be able to use a servlet on the server-side but a specific application listening to a specific TCP port.

渡你暖光 2024-10-04 21:21:24

这听起来很像您重用现有的 URLConnection,而不是为每个请求创建一个新的 URLConnection,并且您通过空 catch 块来抑制异常,并且/或忽略stderr。

对于每个独立请求,您必须创建一个 URLConnection

URL url = new URL("http://example.com");

// First request.
URLConnection connection1 = url.openConnection();
// Process it...

// Second request.
URLConnection connection2 = url.openConnection();
// Process it...

// Etc...

仅当 servlet 在 HttpSession 中存储您希望在后续请求中重新访问的内容时,会话管理才会出现。这里的情况似乎并非如此。

This sounds very much like as if you're reusing an existing URLConnection instead of creating a new one for each request and that you're suppressing the exceptions by empty catch blocks and/or ignoring the stderr.

For each independent request, you have to create a new URLConnection.

URL url = new URL("http://example.com");

// First request.
URLConnection connection1 = url.openConnection();
// Process it...

// Second request.
URLConnection connection2 = url.openConnection();
// Process it...

// Etc...

The session management only comes into picture when the servlet is storing something in the HttpSession which you would like to re-access in the subsequent requests. This doesn't seem to be the case here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文