Created
May 6, 2013 18:30
-
-
Save geekgonecrazy/5527063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Getting resource from server. | |
new getInfo(this).execute("GET", "http://someurl.com/resource"); | |
// Extending and using the ServerComm | |
public class getInfo extends ServerComm { | |
private static final String TAG = "getInfo"; | |
public getInfo(Context context) { | |
super.currentContext = context; | |
} | |
protected void onPostExecute(String result) { | |
Log.d(TAG, result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.KeyManagerFactory; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.TrustManagerFactory; | |
import org.apache.http.util.ByteArrayBuffer; | |
import java.security.KeyManagementException; | |
import java.security.KeyStore; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.security.cert.CertificateException; | |
import android.content.Context; | |
import android.content.res.Resources.NotFoundException; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
public class ServerComm extends AsyncTask<String, Void, String> { | |
private static final String TAG = "NetworkActivities"; | |
Context currentContext; | |
@Override | |
protected String doInBackground(String... options) { | |
// Passphrase for Self Signed sslcert | |
char[] passphrase = "passphrase".toCharArray(); | |
try { | |
// Loading custom keystore | |
KeyStore ksTrust = KeyStore.getInstance("BKS"); | |
ksTrust.load(currentContext.getResources().openRawResource(R.raw.sslcert), passphrase); | |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | |
tmf.init(ksTrust); | |
// Create a SSLContext with the certificate | |
SSLContext sslContext = SSLContext.getInstance("TLS"); | |
sslContext.init(null, tmf.getTrustManagers(), new SecureRandom()); | |
// Create a HTTPS connection | |
URL commandURL = new URL(options[1]); | |
HttpsURLConnection conn = (HttpsURLConnection) commandURL.openConnection(); | |
// Add the SSLContext to the connection | |
conn.setSSLSocketFactory(sslContext.getSocketFactory()); | |
InputStream is = conn.getInputStream(); | |
BufferedInputStream bis = new BufferedInputStream(is); | |
ByteArrayBuffer baf = new ByteArrayBuffer(50); | |
int current = 0; | |
while((current = bis.read()) != -1){ | |
baf.append((byte)current); | |
} | |
return new String(baf.toByteArray()); | |
} catch (KeyStoreException e) { | |
Log.d(TAG, "KeystoreException"); | |
Log.d(TAG, "Exception: "+e.getMessage()); | |
} catch (NoSuchAlgorithmException e) { | |
Log.d(TAG, "NoSuchAlgorithmException"); | |
Log.d(TAG, "Exception: "+e.getMessage()); | |
} catch (CertificateException e) { | |
Log.d(TAG, "CertificateException"); | |
Log.d(TAG, "Exception: "+e.getMessage()); | |
} catch (NotFoundException e) { | |
Log.d(TAG, "NotFoundException"); | |
Log.d(TAG, "Exception: "+e.getMessage()); | |
} catch (IOException e) { | |
Log.d(TAG, "IOException"); | |
Log.d(TAG, "Exception: "+e.getMessage()); | |
} catch (KeyManagementException e) { | |
Log.d(TAG, "KeyManagementException"); | |
Log.d(TAG, "Exception: "+e.getMessage()); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment