java 有时 HttpURLConnection.getInputStream 执行速度太慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1920623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sometimes HttpURLConnection.getInputStream executes too slowly
提问by Volodymyr Bezuglyy
We have next code.
Sometimes we should wait 10-20-40 seconds on the last line.
What can be the problem?
我们有下一个代码。
有时我们应该在最后一行等待 10-20-40 秒。
可能是什么问题?
Java 1.4
爪哇 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();
UPDATED:
Next code fixes the problem IN ECLIPSE.
But it still DOES NOT WORK via Java WebStart:(
更新:
下一个代码修复了 ECLIPSE 中的问题。
但它仍然无法通过 Java WebStart 工作:(
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
System.setProperty("http.keepAlive", "false"); //<---------------
conn.connect();
But why?
但为什么?
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();
Note:
setRequestProperty("Content-Length", "1000")is root cause of the problem.
注意:
setRequestProperty("Content-Length", "1000")是问题的根本原因。
回答by ZZ Coder
'We had a similar issue which is caused by buggy keep-alive in old Java. Add this before connect to see if it helps,
'我们有一个类似的问题,它是由旧 Java 中的错误 keep-alive 引起的。在连接之前添加它以查看它是否有帮助,
conn.setRequestProperty("Connection", "close");
or
或者
System.setProperty("http.keepAlive", "false");
回答by Paling
Had the same problem, found out it was caused by IPv6.
有同样的问题,发现它是由IPv6引起的。
You Disable it from code using:
您可以使用以下代码从代码中禁用它:
System.setProperty("java.net.preferIPv4Stack" , "true");
You can also disable it via the command line using : g-Djava.net.preferIPv4Stack=true
您还可以使用以下命令通过命令行禁用它: g-Djava.net.preferIPv4Stack=true
回答by Reinaldo Santos
I had same problem, so i change to HTTPClient from Apache, follow a example:
我遇到了同样的问题,所以我从 Apache 更改为 HTTPClient,请参考示例:
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("www.myurl-to-read");
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(8000)
.setConnectTimeout(10000)
.setConnectionRequestTimeout(1000)
.build();
request.setConfig(requestConfig);
request.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
回答by Thomas Jung
Try it with an IP address. To see if it's a DNS problem.
用IP地址试试。看看是不是DNS问题。
回答by enguerran
The problem can be something from network sub layer... Should be hard to find it.
问题可能出在网络子层……应该很难找到。
But what about the setReadTimeOut()with low value and a while loop?
但是setReadTimeOut()低值和while循环呢?
回答by Carl Smotricz
One thing I would guess is that your DNS server isn't responding well.
我猜的一件事是您的 DNS 服务器响应不佳。
Can you experiment with changing symbolic domain names to numeric IP addresses before you start? Or can you do each request twice (just for experimentation) and see if the first request is significantly slower than the second?
在开始之前,您可以尝试将符号域名更改为数字 IP 地址吗?或者您可以将每个请求执行两次(仅用于实验)并查看第一个请求是否比第二个请求慢得多?
Google has put up a DNS server at (among others) 8.8.8.8 . They claim it's faster than most other DNS servers. Give that a try!
谷歌已经在(其中包括) 8.8.8.8 建立了一个 DNS 服务器。他们声称它比大多数其他 DNS 服务器更快。试试看!

