java tcp 终止符,Java简单代码:java.net.SocketException:来自服务器的文件意外结束...

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;

}

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.

Has anybody got any ideas?

解决方案

"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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值