陷入java.net.SocketInputStream.socketRead0(本机方法)

时间:2014-09-22 07:31:56

标签: java sockets httpurlconnection urlconnection

我遇到了java.net.SocketInputStream.socketRead0(Native Method)。请看下面的线程转储,它已处于此状态3个小时。

Thread-0" prio=10 tid=0x00007facd02a5000 nid=0x309 runnable [0x00007facd4a43000]
java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
    at sun.security.ssl.InputRecord.read(InputRecord.java:480)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
    - locked <0x00000000e34a0428> (a java.lang.Object)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
    - locked <0x00000000e34a0590> (a sun.security.ssl.AppInputStream)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    - locked <0x00000000e30408b8> (a java.io.BufferedInputStream)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:633)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
    - locked <0x00000000e3031d00> (a sun.net.www.protocol.https.DelegateHttpsURLConnection)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    - locked <0x00000000e3031c80> (a sun.net.www.protocol.https.HttpsURLConnectionImpl)

我已将连接超时和读取超时设置为URLConnection。请参阅下面的代码段。所以我不知道为什么它会挂在java.net.SocketInputStream.socketRead0上。我偶尔会遇到这个问题。任何建议表示赞赏!

public String sendPost(String params) throws Throwable {
PrintWriter out = null;
String htmlContent = null;
try
{
  StringBuffer strBuffer = new StringBuffer();
  URL url = new URL(this.webUrl);
  URLConnection urlConnection = url.openConnection();
  urlConnection.setConnectTimeout(3000);
  urlConnection.setReadTimeout(3000);

  urlConnection.setRequestProperty("accept", "*/*");
  urlConnection.setRequestProperty("connection", "Keep-Alive");
  urlConnection.setRequestProperty("user-agent", "***");

  urlConnection.setDoOutput(true);
  urlConnection.setDoInput(true);

  out = new PrintWriter(urlConnection.getOutputStream());
  out.print(params);
  out.flush();

  BufferedReader bufferdReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
  String readLine = null;
        try {
            while ((readLine = bufferdReader.readLine()) != null) {
                strBuffer.append(readLine);
            }
        } catch (Throwable e) {
            return htmlContent;
        }
        htmlContent = strBuffer.toString();
        bufferdReader.close();

2 个答案:

答案 0 :(得分:2)

我今天在我们的系统上发现了同样的错误。这是JDK中使用的本机实现导致的JDK中的错误。最近于2016年9月21日修复,因此应在以后的JDK版本中提供。

这是错误: https://bugs.openjdk.java.net/browse/JDK-8075484

如果您不能等待,似乎有人创建了一个解决方法: https://github.com/nukesparrow/JNativeSocket

答案 1 :(得分:0)

我认为这是URLConnection的一个错误。使用套接字和超时来检查它们。