HttpsURLConnection和间歇性连接

Oli*_*ank 6 connection android intermittent httpurlconnection

我希望有人可以通过间歇性连接帮助我,我正在使用HttpsURLConnection代码.我正在使用的代码如下:

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setReadTimeout(10 * 1000); 
if conn.getResponseCode() != 200) { 
            Log.v(TAG, "error code:" + conn.getResponseCode()); 
} 
Run Code Online (Sandbox Code Playgroud)

每次我使用它来拉取json文件时,连接都会第一次运行.但是,当我再次使用连接发送命令时,它总是第一次失败.如果我快速发送命令(在5秒内),它通常会起作用,但如果我等待一段时间则失败.我不认为它是SSL问题,因为它第一次正确连接,但我可能在这里错了.我还尝试了许多不同的变体,例如添加:

conn.setUseCaches(false); 
conn.setRequestProperty("Connection","Keep-Alive"); 
conn.getHostnameVerifier(); 
conn.getSSLSocketFactory(); 
conn.setDoOutput(true); 
conn.setDoInput(true); 
conn.setRequestMethod("POST"); 
conn.wait(100); 
Run Code Online (Sandbox Code Playgroud)

但是,我没有运气.任何帮助将不胜感激.

m6t*_*6tt 9

System.setProperty("http.keepAlive", "false");在你做之前先试试

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
Run Code Online (Sandbox Code Playgroud)