Java URLConnection被卡住并且没有抛出错误

Java URLConnection被卡住并且没有抛出错误,java,httpurlconnection,urlconnection,Java,Httpurlconnection,Urlconnection,下面是我的代码。当我运行这段代码时,它总是被卡住。有时它运行循环100次,有时3000次。奇怪的是,它没有抛出错误,只是卡住了。有人有什么想法吗 import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import us.monoid.web.Resty; public class Example1 { /** * @param args the comman

下面是我的代码。当我运行这段代码时,它总是被卡住。有时它运行循环100次,有时3000次。奇怪的是,它没有抛出错误,只是卡住了。有人有什么想法吗

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import us.monoid.web.Resty;


public class Example1 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    // Allow certificates - See http://en.wikibooks.org/wiki/Programming:WebObjects/Web_Services/How_to_Trust_Any_SSL_Certificate
    SSLUtilities.trustAllHostnames( );
    SSLUtilities.trustAllHttpsCertificates() ;

    // Variables
    int i = 0;
    String  askBidURL = "https://www.exchangebitcoins.com/data/depth";

     while(true){ 

       System.out.println("Example 1 - Run #" + i);
       Resty r = new Resty();

       try {String jsonw = r.text(askBidURL).toString();} 
       catch (IOException ex){Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex); }
       i++;

   }
}
}
text()或其中的调用可能无法足够快地接收数据

在从HTTPURLConnection的InputStream执行read()操作(即使中间有一个BufferedInputStream)并逐渐将字节追加到字符串时,我遇到过类似的情况。这显然是由于字符串构建速度太慢,无法跟上传入的数据。通过优化该循环,问题消失了

它会随机发生,大约每100到1000次就有一次;但当它发生时,它会以某种方式破坏以后的连接,即使外部计时器线程close()中断了卡住的read()的流和连接

推测一下,如果循环没有足够快地从InputStream中读取数据,那么传输似乎有时会放弃,因此InputStream的read()永远不会看到大小为零的块,也永远不会完成


不确定你有多大的心情去修改你的库…

这里没有足够的信息来回答这个问题。您声明URLConnection有问题,但在代码中的任何地方都没有出现。还有,会发生什么?您得到的错误或输出是什么?它一定是某种东西,因为你声明它运行循环n次。。。之后会发生什么?循环运行n次后,它就不会移动了。程序不会关闭,也不会引发异常。它只是停留在同一行代码上。