我正在执行以下代码,并且总是在 urlConnection.getInputStream() 中得到 EOFException。我知道网络服务器没有返回正确的 header ,但如果我在网络浏览器中使用相同的 url,我会得到预期的内容。
URL url1;
try
{
url1 = new URL("http://63.255.173.242/get_public_tbl.cgi?A=1");
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
try
{
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
urlConnection.disconnect();
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
有什么想法吗?
编辑 1:
我以为我找到了一个可能的解决方案。我发现使用 Android 2.3 我可以通过调用 getHeaderFields() 读取响应(即使没有 header ),但如果我使用 Android 4.1 getHeaderFields() 返回 null。有什么想法吗?
url1 = new URL("http://63.255.173.242/get_public_tbl.cgi?A=1");
URLConnection urlConnection = url1.openConnection();
Map<String, List<String>> fields = urlConnection.getHeaderFields();
最佳答案
由于您的服务器没有使用正确的 HTTP,您不能使用 URL.openConnection,但您仍然可以使用到 63.255.173.242 端口 80 的普通套接字连接并发送字符串来访问它:
GET/get_public_tbl.cgi?A=1 HTTP/1.0\r\n \r\n
然后读取结果流。
关于java - urlConnection.getInputStream() 抛出 EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12363468/