Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Java HttpClient to connect to RAM server

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.

0 votes



3 answers

Permanent link
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);
...

0 votes


Permanent link
Thank you that worked fine - Can I use a FORM based authentication? If so what url should I specify in the GetMethod?
Thanks

0 votes


Permanent link
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.

0 votes

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details

Question asked: May 02 '10, 10:11 p.m.

Question was seen: 7,661 times

Last updated: May 02 '10, 10:11 p.m.

Confirmation Cancel Confirm