如何重用HttpUrlConnection? [英] How to Reuse HttpUrlConnection?

查看:769
本文介绍了如何重用HttpUrlConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣重用HttpUrlConnection(作为我正在开发的服务器和客户端之间的状态协议的一部分)。
我知道持久性http有一个Connection = keep-alive标头。
现在,我想知道如何重用这样一个问题。
我写了这段代码:

I am interested in reusing an HttpUrlConnection (as part of a statefull protocol between server and client that I'm developing). I know that there is an Connection=keep-alive header for persistent http. Now, I want to know how to reuse such a conenction. I have written this code:

URL u = new java.net.URL("http://localhost:8080/Abc/Def");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Connection", "keep-alive");
c.setHeader("A","B");
c.getInputStream() //here I see that server gets my messages (using DEBUG)
c.setHeader("B","C"); //

现在如何将此B标题重新发送到服务器,我尝试重新连接等等,但没有任何东西可以工作。

Now how do I resend this "B" header to the server, I tried re-connect etc,but nothing gets it to work.

服务器还执行 response.setHeader(Connection,keep-alive);

我看过许多论坛,但没有人写过这个。也许 HttpURLConnection 不处理这个?

I've looked in many forums, but no one wrote about this. Maybe HttpURLConnection doesn't handle this?

推荐答案

你没有。你关闭这个并创建一个新的。它在幕后进行TCP连接池和keepalive。

You don't. You close this one and create a new one. It does TCP connection pooling and keepalive behind the scenes.

这篇关于如何重用HttpUrlConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆