HttpsURLConnection 和间歇性连接

HttpsURLConnection and intermittent connections(HttpsURLConnection 和间歇性连接)
本文介绍了HttpsURLConnection 和间歇性连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我解决断断续续的连接问题使用 HttpsURLConnection 的代码.我正在使用的代码是下面:

I'm hoping someone could help me out with intermittent connections I'm getting using code with HttpsURLConnection. The code I'm using is below:

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

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

The connection works the first time everytime when I use it to pull a json file. However, when I use the connection again to send a command, it always fails the first time. It then typically works if I send the command quickly ( within 5 seconds), but fails if I wait a while. I don't think its a SSL issue because it connects the first time correctly, but I could be wrong here. I also tried many different variations such as adding:

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

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

However, I had no luck. Any help would be greatly appreciated.

推荐答案

先试试 System.setProperty("http.keepAlive", "false");

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

这篇关于HttpsURLConnection 和间歇性连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)