HttpsURLConnection和间歇性连接
我希望有人能够帮助我断断续续的连接我 获取使用HttpsURLConnection代码。我正在使用的代码是 如下:HttpsURLConnection和间歇性连接
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10 * 1000);
if conn.getResponseCode() != 200) {
Log.v(TAG, "error code:" + conn.getResponseCode());
}
的连接工作在第一时间每次,当我用它来拉 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);
但是,我没有运气。任何帮助将不胜感激。
尝试System.setProperty("http.keepAlive", "false");
你做
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
完美时将其设置一次。这工作。我不知道你可以使用“系统”。我猜测SSL连接在我通过不同的活动进行移动之前保持开放。谢谢! – OliverPank 2010-02-04 04:54:39
是的,我认为这与errortream没有被完全读取有关。 – m6tt 2010-02-04 12:14:10
http://code.google.com/p/android/issues/detail?id=2939 应该固定为Froyo。 – 2011-10-21 18:20:17
试试这个代码 - 它非常可靠地对我说:
public static final String USER_AGENT = "Mozilla/5.0 (Linux; U; Android 1.1; en-us;dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";
private DefaultHttpClient getThreadSafeHttpClient() {
final HttpParams params = new BasicHttpParams();
params.setParameter("http.useragent", USER_AGENT);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
final SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", sslSocketFactory, 443));
final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
final DefaultHttpClient httpclient = new DefaultHttpClient(manager, params);
// how to handle retries
final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) {
if (executionCount >= 5) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (exception instanceof SSLHandshakeException) {
// Do not retry on SSL handshake exception
return false;
}
final HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
final boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
return true;
}
return false;
}
};
httpclient.setHttpRequestRetryHandler(myRetryHandler);
return httpclient;
}
谢谢...这比我简单的https url连接复杂得多。我不确定如何正确使用它。由于getThreadSafeHttpClient函数没有任何输入,我在哪里输入我的url? – OliverPank 2010-02-01 02:30:33
你只是用它来获取HttpClient,然后像往常一样使用客户端。类似于 'HttpClient client = getThreadSafeHttpClient();' – Bostone 2010-02-01 04:33:58
HttpGet get = new HttpGet(url); HttpResponse response = this.client.execute(get); – Bostone 2010-02-01 05:17:44
之前只是一点点除了m6tt的回答以上:
private static void disableConnectionReuseIfNecessary() {
// HTTP connection reuse which was buggy pre-froyo
if (!Constants.SUPPORTS_FROYO) {
System.setProperty("http.keepAlive", "false");
}
}
如果我可以建议使用'LogCat'并在你的代码块中放置调试语句,以便它可以向你报告究竟是什么失败, e – 2010-01-31 18:31:41
我从conn.getResponseCode()获得的Logcat响应是“-1”,我搜索到了,并且在https连接的-1响应代码中找不到任何内容。 – OliverPank 2010-01-31 21:40:29