Java Android HTTP URL连接从getResponseCode引发IOException
我正在尝试获取一个URL,我看到下面的行为每次都是一致的和可复制的Java Android HTTP URL连接从getResponseCode引发IOException,java,android,httpurlconnection,ioexception,Java,Android,Httpurlconnection,Ioexception,我正在尝试获取一个URL,我看到下面的行为每次都是一致的和可复制的 当我在我的LTE连接上从我的运营商,一切工作完美没有任何问题 每当我在工作时切换到wifi连接时,我都会从getResponseCode收到一个IO异常。(原因:android.system.ErrnoException:recvfrom失败:ECONNRESET(由对等方重置连接)) 工作时的wi-fi设置为通过防火墙和登录机制。因此,我认为这可能与此有关(即使登录会话有效期约为2天。因此,一旦从设备登录,应该不会出现任何问题
getResponseCode
收到一个IO异常。(原因:android.system.ErrnoException:recvfrom失败:ECONNRESET(由对等方重置连接))getJSon
方法的代码:
public static String getJSON(String url, int timeout) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
System.setProperty("http.keepAlive", "false");
c = (HttpURLConnection) u.openConnection();
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie(u.getHost());
c.setRequestProperty("Cookie", cookie);
if(cookie == null || cookie.isEmpty()) {
Log.i(TAG,"No authentication cookie. Not trying to GET from the self api.");
return ""; //No authentication cookie. GET call will fail.
}
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
Log.i(TAG,"Status Code in getJSON:" + status);
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
default:
Log.e(TAG,"Failure retrieving contents from URL:"+ url +" HTTP code:" + status);
return "";
}
} catch (MalformedURLException ex) {
Log.e(TAG,"Malformed URL:" + url);
} catch (IOException ex) {
Log.e(TAG,"IOException when retrieving user data from URL");
ex.printStackTrace();
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Log.e(TAG,"Exception when disconnecting url resource when retrieving user data from URL.");
}
}
}
return "";
}
异常堆栈跟踪:
05-19 10:20:30.154 18528-20770/com.example W/System.err﹕ java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:556)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at com.android.okio.Okio$2.read(Okio.java:113)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at com.android.okio.RealBufferedSource.indexOf(RealBufferedSource.java:147)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:94)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:491)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.ConnectionHelper.getJSON(ConnectionHelper.java:98)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper.retrieveAndStoreUserData(RegistrationHelper.java:296)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper.registerDeviceWithBackend(RegistrationHelper.java:444)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper.access$400(RegistrationHelper.java:31)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper$1.doInBackground(RegistrationHelper.java:197)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.eclinic247.www.eclinicdr.RegistrationHelper$1.doInBackground(RegistrationHelper.java:177)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ Caused by: android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.Posix.recvfromBytes(Native Method)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.Posix.recvfrom(Posix.java:161)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ ... 24 more
你不认为你应该展示你的代码吗?请发布代码,这样我们就可以查看代码了。尝试不使用Content length属性。已尝试。没有任何区别。同样的行为仍在继续(在运营商网络连接上工作正常,在连接到office wi-fi时引发异常)。由于没有答案,我想将我自己找到的解决方案发布到这个问题上。这是因为公司wifi上设置了防火墙。通过比较来自设备上浏览器和应用程序/本机调用的请求头,我们发现了这一点。由于webview调用在浏览器上进行,我决定使用webview使用的用户代理字符串,并使用httpurlconnection在get/post请求上设置该字符串。现在它工作起来没有任何问题。