开发者

How to make this REST get method in Java?

开发者 https://www.devze.com 2023-04-12 09:39 出处:网络
I have the following REST get Request that works successfully: The result is a XML document that I then want to p开发者_C百科arse. I tried the same in Java:

I have the following REST get Request that works successfully:

How to make this REST get method in Java?

The result is a XML document that I then want to p开发者_C百科arse. I tried the same in Java:

I use the following code:

public void getRootService() throws ClientProtocolException, IOException {

    HttpGet httpGet = new HttpGet("https://localhost:9443/ccm/rootservices");
    httpGet.setHeader("Accept", "text/xml");
    HttpResponse response = client.execute(httpGet);
    HttpEntity entity = response.getEntity();
    InputStream in = entity.getContent();
    String projectURL = XMLDocumentParser.parseDocument(in);

    System.out.println(projectURL);
    HttpGet getProjectsRequest = new HttpGet("https://localhost:9443/ccm/process/project-areas");
    getProjectsRequest.setHeader("Content-Type", "application/xml;charset=UTF-8");
    getProjectsRequest.setHeader("Accept-Charset", "UTF-8");
    getProjectsRequest.setHeader("Accept", "application/xml");


    ResponseHandler<String> handler = new BasicResponseHandler();
    String projectResponse = client.execute(getProjectsRequest, handler);
    //String projectResponse = client.execute(getProjectsRequest, handler);

    System.out.println(projectResponse);

}

But how can I do the authentication? I tried to just add another header field for the value "Authorization" but then I don't get the same result.


I think you have to create a UsernamePasswordCredentials, something along the lines of (untested);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
    new AuthScope("somehost", AuthScope.ANY_PORT), 
    new UsernamePasswordCredentials("username", "password"));

HttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);

See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

Edit:
Just tried the following code and successfully called a REST service on our dev environment that is BASIC protected.

public static void main(String[] args) throws ClientProtocolException, IOException {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
        new AuthScope("dev.*******.com", AuthScope.ANY_PORT), 
        new UsernamePasswordCredentials("*****", "******"));


    DefaultHttpClient client = new DefaultHttpClient();
    client.setCredentialsProvider(credsProvider);

    String url = "http://dev.******.com:18081/path/to/service/id.xml";

    HttpGet get = new HttpGet(url);
    ResponseHandler<String> handler = new BasicResponseHandler();
    String resp = client.execute(get, handler);

    System.out.println(resp);
  }


You can explicitly add Authorization header to HttpRequestBase (an abstract class for HttpGet, HttpPost, etc.)

This example shows how Basic Authorization value is made.

String unhashedString = userName + ":" + password;
String hashedString = Base64.encode(unhashedString); //This class doesn't exist, it's for demonstration purpose only.

HttpGet.setHeader("Authorization", "Basic " + hashedString);

In the server, you will have to do the reverse (if you're doing your own implementation.


The way I implemented this is to use the session with a standard HTTP controller. They call a login URL with user and password posted and I authenticate the session. Once that's done then all following URLs simply check to make sure the session is authenticated.


The authentication mechanism supported will depend on which web server is used to host Rational Team Concert. The Apache Tomcat server uses basic authentication, whereas, WebSphere Application Server supports both basic authentication and form-based authentication.

You can view the detail description here

0

精彩评论

暂无评论...
验证码 换一张
取 消