Android HttpsURLConnection接收不到数据

 公司项目通信用的是Afinal框架,Afinal通信模块是对HttpClient的封装,HttpClient非常强大,但灵活性不够,近段时间比较清闲,于是将项目的通信实现改为HttpsURLConnection。

想了解HttpClient和HttpURLConnection的可参考

http://blog.csdn.net/hguang_zjh/article/details/33743249

http://www.cnblogs.com/devinzhang/archive/2012/01/17/2325092.html


 但是在改造的过程中,遇到一个非常奇葩的问题。

         公司项目有两套后台环境,姑且称为A和B。A是通过局域网访问,基于http协议,B基于https协议通过Internet访问Apache HTTP再转接到A,可以认为A和B为同一套环境,他们的区别仅在于客户端一个是需要连接局域网,一个需要连接外网,并且外网的是用https协议(A是测试环境,B是模拟上线的环境)。在测试环境下,一切皆正常,但是在模拟上线环境上,当某个登录账号被另一台设备登录后,再次发送通信,一直无法返回数据。客户端的异常为


11-10 21:12:14.940: W/System.err(10348): java.net.ProtocolException: content-length promised 182 bytes, but received 0
11-10 21:12:14.950: W/System.err(10348): 	at com.android.okhttp.internal.http.RetryableOutputStream.close(RetryableOutputStream.java:52)
11-10 21:12:14.950: W/System.err(10348): 	at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:636)
11-10 21:12:14.950: W/System.err(10348): 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
11-10 21:12:14.950: W/System.err(10348): 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
11-10 21:12:14.950: W/System.err(10348): 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:143)
11-10 21:12:14.950: W/System.err(10348): 	at java.net.URLConnection.getContentType(URLConnection.java:326)
11-10 21:12:14.950: W/System.err(10348): 	at java.net.URLConnection.getContent(URLConnection.java:193)
11-10 21:12:14.950: W/System.err(10348): 	at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:169)

让后台看了下被挤下去时,数据是如何返回的,结果发现一般数据是通过Spring框架返回的,而当账号被其它设备登录时,返回的数据是由JSP返回。反反复复的将客户端的代码看了几遍,试了各种请求头,还是不行。让IOS客户端截取了返回的数据,但却不是182 bytes。很是苦恼,搁了一天,第二天早上,灵光一闪,打印了上送数据的长度,发现正好是182,于是将urlConn.setRequestProperty("Content-Length",jsonBytes.length + "");这句注掉,发现就可以了,查了很多资料,仍不知为什么,希望有大神能解答。

猜你喜欢

转载自blog.csdn.net/msl0903/article/details/41019067
今日推荐