How to call secured RESTful service in java?
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
...
...
String url = "pankaj-lilhare.blogspot"; /* http://pankaj-lilhare.blogspot:8080/test */
int portNumber = 8080; /* depends on deployed service port */
String username = "test";
String password = "test123";
HttpClient client = null;
try {
client = new HttpClient();
client.getState().setCredentials(
new AuthScope(url, portNumber,AuthScope.ANY_REALM),
new UsernamePasswordCredentials(username, password));
GetMethod method = new GetMethod(restUrl);
int returnCode = client.executeMethod(method);
if ( returnCode>= 200) {
String response = method.getResponseBodyAsString();
}
} catch (Exception e) {
e.printStackTrace();
}
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
...
...
String url = "pankaj-lilhare.blogspot"; /* http://pankaj-lilhare.blogspot:8080/test */
int portNumber = 8080; /* depends on deployed service port */
String username = "test";
String password = "test123";
HttpClient client = null;
try {
client = new HttpClient();
client.getState().setCredentials(
new AuthScope(url, portNumber,AuthScope.ANY_REALM),
new UsernamePasswordCredentials(username, password));
GetMethod method = new GetMethod(restUrl);
int returnCode = client.executeMethod(method);
if ( returnCode>= 200) {
String response = method.getResponseBodyAsString();
}
} catch (Exception e) {
e.printStackTrace();
}
Comments
Post a Comment