Hi list,
i want to implement a client for https, all in my localhost (in my apache,
it is configured to ask authentication), with a resource "
https://localhost:443/resources/resource1.xml"
the server works fine.
I have a client for desktop, and it works fine, the resources are consumed
using httpcomponents-client-4.0.3), i have readed that Android uses that
version.
This is the code of my client in Android (practically is the same of desktop
mapped to android) ,
in the line of SSLContext sc = SSLContext.getInstance("TSL"); the next
exception is thrown :
java.security.NosuchAlgorithmException: SSLContext SSL implementation not
found
Please does anybody know how to solve this issue?
i have tried with TSL and the exception continue.
import java.io.IOException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class androidNativeRest extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
String res="nada";
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("10.0.2.2", 443),
new UsernamePasswordCredentials("user", "pass"));
BasicHttpContext localcontext = new BasicHttpContext();
// Generate BASIC scheme object and stick it to the local
// execution context
BasicScheme basicAuth = new BasicScheme();
localcontext.setAttribute("preemptive-auth", basicAuth);
// Add as the first request interceptor
httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);
HttpHost targetHost = new HttpHost("10.0.2.2", 443, "https");
HttpGet httpget = new HttpGet("/resources/resource1.xml");
res+="executing request: " + httpget.getRequestLine()+"/ln";
res+="to target: " + targetHost+"/ln";
//----------------------TRUST MANAGER---------------------------
//Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[]
getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String
authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String
authType) {
}
}
};
//install trust manager
try {
SSLContext sc = SSLContext.getInstance("TSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
res+="error en trustmanage"+e.toString()+" /ln";
}
//now we can access an https URL without having the certificate in
the truststore
//----------------------------FINISH TRUST MANAGER
INSTALLATION---------
for (int i = 0; i < 3; i++) {
try{
HttpResponse response = httpclient.execute(targetHost,
httpget, localcontext);
HttpEntity entity = response.getEntity();
res+="---------------------------------------- /ln";
res+=response.getStatusLine()+"/ln";
if (entity != null) {
res+="Response content length: " +
entity.getContentLength()+"/ln";
res+=EntityUtils.toString(entity)+"/ln";
res = EntityUtils.toString(entity);
entity.consumeContent();
}
}catch(Exception e1){
res+="errores che";
}
}
httpclient.getConnectionManager().shutdown();
tv.setText(res);
setContentView(tv);
}
static class PreemptiveAuth implements HttpRequestInterceptor {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException
{
AuthState authState = (AuthState) context.getAttribute(
ClientContext.TARGET_AUTH_STATE);
// If no auth scheme avaialble yet, try to initialize it
preemptively
if (authState.getAuthScheme() == null) {
AuthScheme authScheme = (AuthScheme) context.getAttribute(
"preemptive-auth");
CredentialsProvider credsProvider = (CredentialsProvider)
context.getAttribute(
ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
if (authScheme != null) {
Credentials creds = credsProvider.getCredentials(
new AuthScope(
targetHost.getHostName(),
targetHost.getPort()));
if (creds == null) {
throw new HttpException("No credentials for
preemptive authentication");
}
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
}
}
}
}
}
thanks for the answers.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en