need help on https (code for http included)

by Marco Schmitz » Sat, 05 Sep 2009 02:17:38 GMT


Sponsored Links
 hank you droidin,

unfortunately I didnt need the httpclient but the stream.

well, I found a solution :)

btw: this is sdk 1.1 r1 code:

greetings,
marco

------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import lgpl.haustein.Base64Encoder;

import org.apache.log4j.Appender;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

/**
* Hyper
*/
public class Hyper extends Activity {

private static Logger logger = Logger.getLogger(Hyper.class);

private static final String HTTP = "http://www.gmx.de";
private static final String HTTPS = "https://www.verisign.com/";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
logger.debug("START");

try {
SSLContext sc = SSLContext.getInstance("TLS");
scinit(null, new TrustManager[] { new MyTrustManager()
}, new
SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new
MyHostnameVerifier());
HttpsURLConnection con = (HttpsURLConnection) new
URL(HTTPS).openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.connect();

BufferedReader br = new BufferedReader(new
InputStreamReader(con.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null)
sb.append(line);

logger.debug("InputStream: " + sb.toString());

logger.debug("END");
} catch (Exception e) {
logger.error(e);
}
}

/**
* MyHostnameVerifier
*/
private class MyHostnameVerifier implements HostnameVerifier {

@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}

/**
* MyTrustManager
*/
private class MyTrustManager implements X509TrustManager {

@Override
public void checkClientTrusted(X509Certificate[] chain, String
authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String
authType) {
}




Other Threads

1. Android 2.0.1 Platform source code

Hi,

How would i get the Android 2.0.1 Platform </sdk/android-2.0.1.html> SDK.

-- 
Thank & Regards
Peeyush Varshney

-- 

2. Receive a notification when a call is answered

I am creating an application for API Level 7 and above that receives a
notification every time a phone call is answered and logs that
information to a sqlLite databas.  When I run the application in the
emulator, I never receive the notifications.

Here is the class i created.  It's simple logs a message to logCat
when it receives the intent:

package ed.app.commTracker;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import ed.app.messageLog.systemOutput;
import java.lang.String;

public class callReceiver extends BroadcastReceiver {
        private final String ACTION_ANSWER ="android.intent.action.ANSWER";


        @Override
        public void onReceive(Context context, Intent intent)
        {
                systemOutput    m_systemLog;
                m_systemLog = new systemOutput(systemOutput.LOG_ALL,
"commTracker" );
                m_systemLog.logMessage("Log Started: CallReceiver");
                m_systemLog.logDebug("CallReceiver::onReceive()");



        }

}


and Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ed.app.commTracker"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/
app_name">
      <activity android:label="@string/app_name" android:name="main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

                <receiver android:name="callReceiver">
                        <intent-filter>
                        <action android:name="android.intent.action.ANSWER" />
                        </intent-filter>
                </receiver>

    </application>
</manifest>

I'm sure I'm doing something wrong, but I can't figure out what.  Any
help is appreciated.  Thanks

-- 

3. Cursor Management with ListView

4. Android: first open and free mobile platform?

5. HTC launches 1.5GHz, 7-inch Android 2.4 Flyer into the tablet wars

6. Closing Cursors

7. XOOM vs G-Slate vs G-Tab 10.1