来自服务器的意外文件结尾

来自服务器的意外文件结尾

本文介绍了Java 简单代码:java.net.SocketException:来自服务器的意外文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java写了一些简单的代码,该方法应该连接到网站并返回BufferedReader.

I wrote some simple code in Java, the method should connect to the website and return the BufferedReader.

private BufferedReader getConnection(String url_a) {
        URL url;
        try {
            System.out.println("getting connection");
            url = new URL(url_a);
            HttpURLConnection urlConnection = (HttpURLConnection)
                     url.openConnection();
            urlConnection.addRequestProperty("User-Agent",
                    "Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924"
                     + "Epiphany/1.4.4 (Ubuntu)");
            inStream = new InputStreamReader(urlConnection.getInputStream());
            return new BufferedReader(inStream);
        } catch (Exception ex) {
            Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

}

当我在我的 PC 上使用它时,它工作正常,但是当我将 .jar 文件放在服务器上时,我收到此错误:

When I use it on my PC, it works fine but when I put .jar file on the server I get this error:

java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:718)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:715)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
at dataconverter.Reader.getConnection(Reader.java.260)

问题很奇怪,因为不是每次都抛出异常,有时一切正常,程序运行正常.

Problem is quite strange because the exception isn't thrown each time, sometimes everything is OK and program works fine.

有人有什么想法吗?

推荐答案

"Unexpected end of file" 暗示远程服务器接受并关闭连接而不发送响应.可能是远程系统太忙而无法处理请求,或者存在随机断开连接的网络错误.

"Unexpected end of file" implies that the remote server accepted and closed the connection without sending a response. It's possible that the remote system is too busy to handle the request, or that there's a network bug that randomly drops connections.

也有可能是服务器中存在错误:请求中的某些内容导致了内部错误,服务器只是关闭了连接,而不是像它应该发送的那样发送 HTTP 错误响应.一些人认为这是由请求中缺少标头或无效标头值引起的.

It's also possible there is a bug in the server: something in the request causes an internal error, and the server simply closes the connection instead of sending a HTTP error response like it should. Several people suggest this is caused by missing headers or invalid header values in the request.

根据现有的信息,不可能说发生了什么问题.如果您有权访问有问题的服务器,您可以使用数据包嗅探工具来查找发送和接收的确切内容,并查看服务器进程的日志以查看是否有任何错误消息.

With the information available it's impossible to say what's going wrong. If you have access to the servers in question you can use packet sniffing tools to find what exactly is sent and received, and look at logs to of the server process to see if there are any error messages.

这篇关于Java 简单代码:java.net.SocketException:来自服务器的意外文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:54