简体   繁体   English

使用HttpURLConnection标头时的Android EOFException

[英]Android EOFException when using HttpURLConnection headers

I sign my http request with a custom authorization header: 我使用自定义授权标头签署我的http请求:

String key="client="+USER+",hash="+sha1(STR, API_KEY)+",timestamp="+t.toString();

If anybody is interested in the sha1 method: http://pastebin.com/HRFXQ4Bk The key string is used as header: 如果有人对sha1方法感兴趣: http//pastebin.com/HRFXQ4Bk密钥字符串用作标题:

URL url = new URL(sb.toString());

HttpURLConnection conn = null;  
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", key);
conn.setRequestMethod("GET");

InputStreamReader in = new InputStreamReader(conn.getInputStream());

When I try to get the response I get following error: 当我尝试获得响应时,我得到以下错误:

10-28 18:25:40.111: E/error(6855): java.io.EOFException 10-28 18:25:40.111: E/error(6855): at libcore.io.Streams.readAsciiLine(Streams.java:203) 10-28 18:25:40.111: E/error(6855): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579) 10-28 18:25:40.111: E/error(6855): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827) 10-28 18:25:40.111: E/error(6855): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283) 10-28 18:25:40.111: E/error(6855): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 10-28 18:25:40.111:E / error(6855):java.io.EOFException 10-28 18:25:40.111:E / error(6855):at libcore.io.Streams.readAsciiLine(Streams.java: 203)10-28 18:25:40.111:E / error(6855):at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)10-28 18:25:40.111:E / error(6855) :at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827)10-28 18:25:40.111:E / error(6855):at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283 )10-28 18:25:40.111:E / error(6855):at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)

On my server no access was logged by this request. 在我的服务器上,此请求未记录任何访问权限。 However when I remove the auth header a connection to my server is established according to the server log. 但是,当我删除auth标头时,根据服务器日志建立与我的服务器的连接。

So how does the auth header influence the request? 那么auth头如何影响请求呢? Why is there no connection when using header? 使用标题时为什么没有连接?


BTW a header like BTW像一个标题

conn.setRequestProperty("Authorization", "FOOBAR");

works, however is refused because the authorization header does not match the requirements: 然而,由于授权标头与要求不匹配,因此被拒绝:

10-29 08:12:07.235: E/error(23663): java.net.ConnectException: failed to connect to api.myserver.net (port 1337): connect failed: ECONNREFUSED (Connection refused) 10-29 08:12:07.235: E/error(23663): at libcore.io.IoBridge.connect(IoBridge.java:114) 10-29 08:12:07.235: E/error(23663): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 10-29 08:12:07.235: E/error(23663): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 10-29 08:12:07.235:E / error(23663):java.net.ConnectException:无法连接到api.myserver.net(端口1337):连接失败:ECONNREFUSED(连接被拒绝)10-29 08:12 :07.235:E / error(23663):at libcore.io.IoBridge.connect(IoBridge.java:114)10-29 08:12:07.235:E / error(23663):at java.net.PlainSocketImpl.connect( PlainSocketImpl.java:192)10-29 08:12:07.235:E / error(23663):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)

My webservice requires the header to have following format 我的webservice要求标题具有以下格式

match(/client=([^,]*),hash=([^,]*),timestamp=([^,]*)/);

So this exception is different from the initial exception. 所以这个例外与最初的例外不同。 When I remove the header and disable authorization on my webservice the connection works as expected. 当我删除标题并禁用我的webservice上的授权时,连接按预期工作。 So the problem seems to be with the custom authorization header. 所以问题似乎与自定义授权标头有关。 Any ideas? 有任何想法吗?

Looks like it may be a problem with certain versions of Android according to this question . 看起来它可能是根据这个某些Android版本中的问题的问题

Try adding conn.setRequestProperty("Connection", "close") to your request. 尝试将conn.setRequestProperty("Connection", "close")到您的请求中。

First, check whether your URL contains unexpected newline ('\\n' or '\\r\\n'), if does, you will get EOFException when read response. 首先,检查您的URL是否包含意外的换行符('\\ n'或'\\ r \\ n'),如果有,您将在读取响应时获得EOFException。 The newline will trunk HTTP packages and server thinks client has more data to send, so there is no response. 新行将中继HTTP包,服务器认为客户端有更多数据要发送,因此没有响应。 Every attempt to read response will get EOF immediately. 每次阅读响应的尝试都会立即获得EOF。

After ensure your request is valid, then try solutions provided by other guys. 确保您的请求有效后,请尝试其他人提供的解决方案。

I started to see that problem when I changed the backend of my Android app from Asp to PHP. 当我将Android应用程序的后端从Asp更改为PHP时,我开始发现问题。

I solved it adding the following line on my PHP page: 我解决了它在我的PHP页面上添加以下行:

header("Connection: close");

It might just be due to your server response in that case not quite being correct. 可能仅仅是因为您的服务器响应在这种情况下并不完全正确。

My server was using python SimpleHTTPServer and I was wrongly assuming all I needed to do to indicate success was the following: 我的服务器使用的是python SimpleHTTPServer,我错误地假设我需要做的就是表明成功如下:

self.send_response(200)

That sends the initial response header line, a server and a date header, but leaves the stream in the state where you are able to send additional headers too. 它发送初始响应标题行,服务器和日期标题,但使流处于您能够发送其他标题的状态。 HTTP requires an additional new line after headers to indicate they are finished. HTTP需要在标题之后添加一个新行以表示它们已完成。 It appears if this new line isn't present when you attempt to get the result body InputStream or response code etc with HttpURLConnection then it throws the EOFException (which is actually reasonable, thinking about it). 当你尝试使用HttpURLConnection获取结果体InputStream或响应代码等时,如果这个新行不存在,那么它会抛出EOFException(这实际上是合理的,考虑它)。 Some HTTP clients did accept the short response and reported the success result code which lead to me perhaps unfairly pointing the finger at HttpURLConnection. 一些HTTP客户端确实接受了短响应,并报告了成功结果代码,这导致我可能不公平地将手指指向HttpURLConnection。

I changed my server to do this instead: 我改变了我的服务器来做这件事:

self.send_response(200)
self.send_header("Content-Length", "0")
self.end_headers()

No more EOFException with that code. 该代码不再有EOFException。 It's possible the "Connection: close" solutions trigger some behaviour on certain servers that might work around this (eg ensuring the response is valid before closing) but that wasn't the case with the python SimpleHTTPServer, and the root cause turned out to be my fault. “连接:关闭”解决方案可能会在某些可能解决此问题的服务器上触发某些行为(例如,确保响应在关闭之前有效)但python SimpleHTTPServer不是这种情况,根本原因是我的错。

NB: There are some bugs on Android pre-Froyo (2.2) relating to keep-alive connections but I don't think this is one of them. 注意:Android之前的Froyo(2.2)存在一些与保持连接有关的错误,但我不认为这是其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android的HttpURLConnection在HEAD请求上抛出EOFException - Android's HttpURLConnection throws EOFException on HEAD requests 使用java.net.HttpURLConnection时是否可以转储HTTP标头? - Is it possible to dump the HTTP Headers when using java.net.HttpURLConnection? 在 android 应用程序中读取对象时出现 EOFException - EOFException when readObject in an android app 无法在Android WebView中设置HttpURLConnection标头 - Unable to set HttpURLConnection headers in Android WebView 使用HTTPUrlConnection时出现StackOverflowError - StackOverflowError when using HTTPUrlConnection 使用没有限制头的HttpURLConnection进行测试 - Test using HttpURLConnection without restricted headers 将HttpUrlConnection与针对SDK 21的Android编译时出现错误 - Getting error when using HttpUrlConnection with Android compiling against SDK 21 如何在android中使用HttpURLConnection时提取Chunked数据 - How to Extract Chunked data when using HttpURLConnection in android 使用HttpURLConnection在Android中进行摘要式身份验证 - Digest authentication in Android using HttpURLConnection 使用Android HttpURLConnection 获取Http - Http Get using Android HttpURLConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM