相关疑难解决方法(0)

安全使用HttpURLConnection

当使用HttpURLConnection时,如果我们不"获取"并使用它,则需要关闭InputStream吗?

即这样安全吗?

HttpURLConnection conn = (HttpURLConnection) uri.getURI().toURL().openConnection();
conn.connect();
// check for content type I don't care about
if (conn.getContentType.equals("image/gif") return; 
// get stream and read from it
InputStream is = conn.getInputStream();
try {
    // read from is
} finally {
    is.close();
}
Run Code Online (Sandbox Code Playgroud)

其次,在完全读取所有内容之前关闭InputStream是否安全?

是否存在将底层套接字置于ESTABLISHED或甚至CLOSE_WAIT状态的风险?

java url network-programming http

47
推荐指数
4
解决办法
8万
查看次数

如何重用HttpUrlConnection?

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

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"); //
Run Code Online (Sandbox Code Playgroud)

现在我如何重新发送这个"B"标头到服务器,我尝试重新连接等,但没有任何东西让它工作.

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

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

java servlets http httpurlconnection

27
推荐指数
1
解决办法
2万
查看次数

使用Spring Rest Template时,HttpConnection的默认保持活动时间

我想知道在通过Spring rest Template创建新连接之前,HttpConnection在不活动时保持活动多长时间.我查看了默认的连接超时和读取超时参数,但我相信这些是在由于某些故障等原因未建立连接时在连接超时的上下文中使用的.

我正在寻找的是,如果没有活动(或)没有活动,连接保持多长时间,以及如何通过Spring Rest Template(或)底层机制来配置它.

java spring httpurlconnection resttemplate

5
推荐指数
1
解决办法
1万
查看次数