If you think the Android project bitfynd-wallet-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2013-2014 the original author or authors.
*/*fromwww.java2s.com*/
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/package de.schildbach.wallet.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyStore;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.content.res.AssetManager;
import com.google.common.base.Charsets;
import de.schildbach.wallet.Constants;
/**
* @author Andreas Schildbach
*/publicabstractclass HttpGetThread extends Thread
{
privatefinal AssetManager assets;
privatefinal String url;
@CheckForNull
privatefinal String userAgent;
privatestaticfinal Logger log = LoggerFactory.getLogger(HttpGetThread.class);
public HttpGetThread(@Nonnull final AssetManager assets, @Nonnull final String url, @Nullable final String userAgent)
{
this.assets = assets;
this.url = url;
this.userAgent = userAgent;
}
@Override
publicvoid run()
{
HttpURLConnection connection = null;
log.debug("querying \"" + url + "\"...");
try
{
connection = (HttpURLConnection) new URL(url).openConnection();
if (connection instanceof HttpsURLConnection)
{
final InputStream keystoreInputStream = assets.open("ssl-keystore");
final KeyStore keystore = KeyStore.getInstance("BKS");
keystore.load(keystoreInputStream, "password".toCharArray());
keystoreInputStream.close();
final TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(keystore);
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
((HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory());
}
connection.setInstanceFollowRedirects(false);
connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS);
connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS);
connection.setRequestProperty("Accept-Charset", "utf-8");
if (userAgent != null)
connection.addRequestProperty("User-Agent", userAgent);
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
finallong serverTime = connection.getDate();
// TODO parse connection.getContentType() for charset
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8), 64);
final String line = reader.readLine().trim();
reader.close();
handleLine(line, serverTime);
}
}
catch (final Exception x)
{
handleException(x);
}
finally
{
if (connection != null)
connection.disconnect();
}
}
protectedabstractvoid handleLine(@Nonnull String line, long serverTime);
protectedabstractvoid handleException(@Nonnull Exception x);
}