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
That would be greatly appreciated...
Thanks in advance for sharing.
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
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);
...
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 thelogin link at the right top corner.
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