有时HttpURLConnection.getInputStream的执行速度太慢 [英] Sometimes HttpURLConnection.getInputStream executes too slowly

查看:2873
本文介绍了有时HttpURLConnection.getInputStream的执行速度太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有下一个代码。

有时我们应该在最后一行等待10-20-40秒。

有什么问题?

We have next code.
Sometimes we should wait 10-20-40 seconds on the last line.
What can be the problem?

Java 1.4

URL url = ...;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.connect();
OutputStream out = conn.getOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(out);
try
{
   outStream.writeObject(objArray);
}
finally
{
   outStream.close();
}

InputStream input = conn.getInputStream();

更新:

下一代码解决问题IN ECLIPSE。< br>
但它仍然无法通过Java WebStart工作:(

HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
conn.setDoInput(true);  
conn.setDoOutput(true);  
conn.setUseCaches(false);  
System.setProperty("http.keepAlive", "false");  //<---------------
conn.connect();  

但为什么?

再次更新!

错误已修复!:)



我们使用的连接不是在一个类中而是在两个类中。

第二类中有以下行:

UPDATED one more time!
Bug was fixed! :)

We worked with connections not in one class but in two.
And there is following line in the second class:

URL url = ...  
HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
conn.setRequestProperty("Content-Length", "1000");  //<------------
conn.connect();  

注意:
setRequestProperty(Content-Length, 1000)是问题的根本原因。

Note: setRequestProperty("Content-Length", "1000") is root cause of the problem.

推荐答案

'我们遇到了类似的问题由旧Java中的buggy keep-alive引起的。在连接之前添加它以查看它是否有帮助,

'We had a similar issue which is caused by buggy keep-alive in old Java. Add this before connect to see if it helps,

conn.setRequestProperty("Connection", "close");

System.setProperty("http.keepAlive", "false");

这篇关于有时HttpURLConnection.getInputStream的执行速度太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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