You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Richard Jose <ri...@rediffmail.com> on 2011/03/30 04:06:45 UTC
Establishing a session with REST API using httpclient 4.1
Hello everyone,
Below is a simple code I am trying to run to consume a REST service
with Apache's httpclient 4.1. I was of the opinion authscope would take
care of the sessions but I was obviously wrong Can someone please point
out what I am missing. Kind of stuck. Session keeps getting expired
bbetween REST requests.
//Main Class
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* A simple example that uses HttpClient to execute an HTTP request
against
* a target site that requires user authentication.
*/
public class ClientAuthentication {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient =
HttpClientIgnoreInvalidSSLCertificate.createClient();
//DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("dam-vchg01.land.virtual", 443),
new UsernamePasswordCredentials("rjose",
"rjose6345"));
HttpGet httpget = new HttpGet("https://dam-
vchg01.land.virtual/vCenter-CB/api/costModels/");
System.out.println("executing request" +
httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("---------------------------------------
-");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " +
entity.getContentLength());
try {
BufferedReader reader = new BufferedReader(
new
InputStreamReader(response.getEntity().getContent()));
String line = "";
System.out.println("nHere's the response:n");
while ((line=reader.readLine())!= null)
{
System.out.println(line);
}
} catch (IOException ex) {
ex.printStackTrace();
throw ex;
} catch (RuntimeException ex) {
httpget.abort();
throw ex;
} finally {
//Nothing
}
}
EntityUtils.consume(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
//Another Helper Class
import org.apache.http.impl.client.DefaultHttpClient;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
//import org.apache.http.conn.ssl.X509HostnameVerifier;
public class HttpClientIgnoreInvalidSSLCertificate {
public static DefaultHttpClient createClient() {
try {
DefaultHttpClient base = new DefaultHttpClient();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
//ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
;
SSLSocketFactory ssf = new
SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
ClientConnectionManager ccm = base.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
return new DefaultHttpClient(ccm, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
Console Output:
executing requestGET https://dam-vchg01.land.virtual/vCenter-
CB/api/costModels/ HTTP/1.1
----------------------------------------
HTTP/1.1 200 OK
Response content length: 318
Here's the response:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="failure">
Someone please help me or atleast point me in the right direction as to
how I can access this REST service without having to provide the
authentication details over and over and without getting session
expired error.
Note: Actually the REST response should be an xml.
Thanks,
Richard
Re: Establishing a session with REST API using httpclient 4.1
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2011-03-30 at 02:06 +0000, Richard Jose wrote:
> Hello everyone,
>
> Below is a simple code I am trying to run to consume a REST service
> with Apache's httpclient 4.1. I was of the opinion authscope would take
> care of the sessions but I was obviously wrong
HTTP authentication and HTTP state management are not the same thing.
Unlike form based authentication HTTP auth schemes such as BASIC and
DIGEST do not imply or require a stateful session.
> Can someone please point
> out what I am missing. Kind of stuck. Session keeps getting expired
> bbetween REST requests.
>
...
>
> Someone please help me or atleast point me in the right direction as to
> how I can access this REST service without having to provide the
> authentication details over and over and without getting session
> expired error.
>
> Note: Actually the REST response should be an xml.
>
A wire / context log of the HTTP session might help.
Do you execute requests within the same HTTP context, as described here?
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d4e974
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org