3

I've been scratching my head on this issue for the last few days now. I'm trying to POST data to a server which has an SSL certificate installed.

I've been using the EasySSLSocketFactory and the EasyX509TrustManager classes found here ( http://code.google.com/p/transdroid/source/browse/trunk/src/org/xmlrpc/android/?r=103 ) in conjunction with the following code:

StringEntity se = null;
try {
    se = new StringEntity(tripArray.toString());
} catch (UnsupportedEncodingException e) { }

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

HttpParams params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);
this.httpClient = new DefaultHttpClient(cm, params); 

HttpPost httpPost = new HttpPost("https://xxx");
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

ResponseHandler responseHandler = new BasicResponseHandler();
try {
    String response = this.httpClient.execute(httpPost, responseHandler).toString();
} catch (ClientProtocolException e) {
    tripFail = true;
} catch (IOException e) {
    tripFail = true;
}

But I seem to be getting the following back from my requests:

Thread [<10> Timer-1] (Suspended (exception NullPointerException))  
    Class.forName(String, boolean, ClassLoader) line: 234   
    Class.forName(String) line: 181 
    X509Certificate.<clinit>() line: 60 
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).verifyCertificateChain(byte[][], String) line: 658  
    NativeCrypto.SSL_do_handshake(int, FileDescriptor, NativeCrypto$SSLHandshakeCallbacks, int, boolean) line: not available [native method]    
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).startHandshake(boolean) line: 474   
    OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl) line: 750    
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).getInputStream() line: 692  
    SocketInputBuffer.<init>(Socket, int, HttpParams) line: 98  
    DefaultClientConnection(SocketHttpClientConnection).createSessionInputBuffer(Socket, int, HttpParams) line: 83  
    DefaultClientConnection.createSessionInputBuffer(Socket, int, HttpParams) line: 170 
    DefaultClientConnection(SocketHttpClientConnection).bind(Socket, HttpParams) line: 106  
    DefaultClientConnection.openCompleted(boolean, HttpParams) line: 129    
    DefaultClientConnectionOperator.openConnection(OperatedClientConnection, HttpHost, InetAddress, HttpContext, HttpParams) line: 171  
    SingleClientConnManager$PoolEntry(AbstractPoolEntry).open(HttpRoute, HttpContext, HttpParams) line: 164 
    SingleClientConnManager$ConnAdapter(AbstractPooledConnAdapter).open(HttpRoute, HttpContext, HttpParams) line: 119   
    DefaultRequestDirector.execute(HttpHost, HttpRequest, HttpContext) line: 359    
    DefaultHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, HttpContext) line: 555 
    DefaultHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, ResponseHandler, HttpContext) line: 653    
    DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest, ResponseHandler, HttpContext) line: 627   
    DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest, ResponseHandler) line: 616    
    DataSender.sendData() line: 151 
    DataSender$1.run() line: 202    
    Timer$TimerImpl.run() line: 284 

Does anyone know what could be causing this? I'm a bit stumped on this..

Edit:

I think the issue lies here

Class.forName(String, boolean, ClassLoader) line: 234   
    Class.forName(String) line: 181 

The code seems to be passing a null string to the Class.forName() function, causing it to bail out.. But I'm not sure what's calling that and why.

Thanks, Oli

MrNorm
  • 406
  • 4
  • 14
  • `https://xxx` -> Are you using a real server instead of this one? – Caner Oct 12 '11 at 15:22
  • What if you try to modify the EasySslSocketFactory to use SSLContext.getInstance("Default") instead of SSLContext.getInstance("TLS")? – n3utrino Oct 12 '11 at 15:39
  • @LAS_VEGAS Hah, yeah I'm using a server that had a valid SSL certificate installed – MrNorm Oct 13 '11 at 08:12
  • @gabe Tried that but was told I couldn't use the default instance. I think the issue lies here Class.forName(String, boolean, ClassLoader) line: 234 Class.forName(String) line: 181 The code seems to be passing a null string to the Class.forName() function, causing it to bail out.. But I'm not sure what's calling that and why. – MrNorm Oct 13 '11 at 08:13
  • Maybe the X509Certificate class was not found? – Sibbo Oct 13 '11 at 08:22
  • X509Certificate.() indicates it happens on initialization of the X509Certificate interface. It seems like the X509CertificateImpl (or something like that) is missing. – n3utrino Oct 13 '11 at 08:30
  • It does look like it cannot find it, but I've tried running this on a native android environment (just incase it's my ROM) and I appear to be having the same issue :/ – MrNorm Oct 13 '11 at 11:35

2 Answers2

0

After a long discussion the solution is

use IntelliJ IDEA ;-)

n3utrino
  • 2,361
  • 3
  • 22
  • 32
  • Thanks for the answer. I don't think I need to set stuff like that on the Android platform, unless I'm missing something? – MrNorm Oct 13 '11 at 11:34
  • is it only a problem in the emulator or on the device too? – n3utrino Oct 13 '11 at 11:36
  • Seems to be both. I've also created a new project with just the code from answer #1 of this thread in the main activity: http://stackoverflow.com/questions/2012497/accepting-a-certificate-for-https-on-android .. But I'm getting the same issue in both the emulator and my native device. I must be doing something wrong, or there is a problem with the way my environment is set up .. Totally stumped. – MrNorm Oct 13 '11 at 11:41
  • Did you try different android versions 1.6 - 2.3? – n3utrino Oct 13 '11 at 11:44
  • I once had a problem with the classloader, not finding classes that were perfectly available. The issue was that somewhere I did create a new classloader in the code and it couldnt access all of the classes. I had to pass the classloader of my activity down to where I created my new classloader. Are you using the third party code as a library? maybe you could try to move the code to the package namespace of your main app. – n3utrino Oct 13 '11 at 12:37
  • I did think it could have been how my project has been set up.. But yesterday I created a new project with the above code (stripping out the additional 'easy' classes, too) in the main activity, which produced the same results. Could it be something with my development environment? I can't see how this could happen on a bare project, too. – MrNorm Oct 14 '11 at 09:30
  • I'd be more than happy to zip up the code and upload it somewhere if you wanted to have a look? – MrNorm Oct 14 '11 at 10:13
  • Thanks, I really appreciate it. Here you go: http://dl.dropbox.com/u/25486589/TestHttps.zip .. I've made use of a helper, which produces the same result anyway. – MrNorm Oct 14 '11 at 11:01
  • Works just perfect. I'm getting HTTP_RESPONSE_ERROR - SSLException hostname in certificate didn't match: != as expected. I tried with 2.2 and 2.3 – n3utrino Oct 14 '11 at 11:21
  • What the .. It must be the way I'm building it or something.. What IDE/OS are you using? – MrNorm Oct 14 '11 at 11:25
  • i am using intellij idea community edition on osx and windows – n3utrino Oct 14 '11 at 11:34
  • Seems to work fine via intellij idea community edition for OSX. It must have been the way Eclipse was compiling it? Thanks for all your help. I'm so happy to finally get to the bottom of this. – MrNorm Oct 14 '11 at 12:10
  • You're welcome. Maybe Eclipse was messing with the roms or omitting some libraries. If you want to continue using Eclipse setting up the project from scratch could help. I changed my solution ;-) Happy Coding – n3utrino Oct 14 '11 at 12:27
0

Looks like Eclipse was causing the issue for me. As gabe suggested, Intellij idea community edition compiles the code fine and doesn't cause the NullPointerException. I have no idea what could be causing it and no idea how to fix it. I'm just going to use a different IDE from now on.

MrNorm
  • 406
  • 4
  • 14