It's all about the answers!

Ask a question

Java HttpClient to connect to RAM server


m sawires (1462268) | asked May 02 '10, 10:11 p.m.
Hi,
I'm looking for a simple Java http client to be able to connect to a RAM server in order to read asset via REST API (OSLC). Does anyone has a code snippet to share to connect to a RAM server using the class org.apache.commons.httpclient.HttpClient?
I'm trying to define a login method in a subclass of HttpClient such as

GetMethod get = new GetMethod("http://localhost:13080/ram/secure/loginProxy.faces");

int responseCode = executeMethod(get);

PostMethod post = new PostMethod("http://localhost:13080/ram");

NameValuePair[] nvps = new NameValuePair[2];
nvps[0] = new NameValuePair("j_username", user);
nvps[1] = new NameValuePair("j_password", password);
post.addParameters(nvps);
responseCode = executeMethod(post);



That would be greatly appreciated...
Thanks in advance for sharing.

3 answers



permanent link
Gili Mendel (1.8k56) | answered May 09 '10, 11:04 a.m.
JAZZ DEVELOPER
You can, but you will have to do the regular form based authentication dance. This includes handling redirects http://hc.apache.org/httpclient-3.x/redirects.html


A good way follow the flow, is to use something like Live Headers https://addons.mozilla.org/en-US/firefox/addon/3829 and follow the login link at the right top corner.

permanent link
m sawires (1462268) | answered May 03 '10, 10:32 a.m.
Thank you that worked fine - Can I use a FORM based authentication? If so what url should I specify in the GetMethod?
Thanks

permanent link
Kevin Bauer (34621) | answered May 03 '10, 8:42 a.m.
JAZZ DEVELOPER
Hi,
I'm looking for a simple Java http client to be able to connect to a RAM server in order to read asset via REST API (OSLC). Does anyone has a code snippet to share to connect to a RAM server using the class org.apache.commons.httpclient.HttpClient?
I'm trying to define a login method in a subclass of HttpClient such as

GetMethod get = new GetMethod("http://localhost:13080/ram/secure/loginProxy.faces");

int responseCode = executeMethod(get);

PostMethod post = new PostMethod("http://localhost:13080/ram");

NameValuePair[] nvps = new NameValuePair[2];
nvps[0] = new NameValuePair("j_username", user);
nvps[1] = new NameValuePair("j_password", password);
post.addParameters(nvps);
responseCode = executeMethod(post);



That would be greatly appreciated...
Thanks in advance for sharing.


One way to do this is to go against the web services WAR where you can use BASIC authentication (instead of the FORM based auth I think you are trying to do here).


HttpClient httpClient = new HttpClient();
//Set the userid and password
Credentials credentials = new UsernamePasswordCredentials(USER, PASSWORD);
httpClient.getState().setCredentials(AuthScope.ANY, credentials);

//Perform the HTTP call
GetMethod getAssetDetailsMethod = new GetMethod("http://localhost:13080/ram.ws/oslc/assets/GUID/version");
int responseCode = executeMethod(getAssetDetailsMethod);
...

Your answer


Register or to post your answer.