Reputation: 1564
I am trying to access QC 11 using Rest api in Java.
I am following API reference in the manual provided by HP. Following is basic step for login authentication.
Non-Web Application Authorization Client queries the is-authenticated resource and sends no authentication headers. This step is optional.
GET /qcbin/rest/is-authenticated
Server refuses request and returns reference to authentication point.
HTTP/1.1 401 Unauthorized WWW-Authenticate: LWSSO realm=http://[server]:[port]/qcbin/authentication-point
Client sends a valid Basic Authentication header to the authentication point.
GET /qcbin/authentication-point/authenticate Authorization: Basic ABCDE123
Server validates the Basic authentication headers, creates a new LW-SSO token and returns it as LWSSO_COOKIE_KEY.
HTTP/1.1 200 OK Set-Cookie: LWSSO_COOKIE_KEY={cookie}
The application can now access data and services using the token. At the end of the session, log off to discard the token.
Here is my java code.
DefaultHttpClient httpClient = new DefaultHttpClient();
String encoding = Base64.encodeBase64String("demoUser:demoUser123".getBytes());
HttpGet httpGet = new HttpGet("http://HOST_VALUE:PORT_VALUE/qcbin/authentication-point/authenticate");
//httpGet.setHeader("GET", "/qcbin/authentication-point/authenticate");
httpGet.setHeader("Authorization:", "Basic " + encoding);
HttpResponse response;
httpClient.getCredentialsProvider().setCredentials(
new AuthScope("proxyHost", 8080),
new UsernamePasswordCredentials("userName", "Password"));
response = httpClient.execute(httpGet);
System.out.println(response.getAllHeaders().toString());
System.out.println(response.getStatusLine().toString());
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
It gives me output as
[Lorg.apache.http.Header;@159e154 HTTP/1.1 400 Bad Request Output from Server ....
I am new to REST by using Java. Can anyone help? Any examples for connecting to ALM using REST and fetching data?
Upvotes: 3
Views: 5862
Reputation: 1564
Solved the problem.
The problem was in base64 encoding ! If you are converting string to base64 encoded string... if result is greater than 76 char. it adds new line ! even if its less than 76
So solution was
encoding = encoding.replaceAll("\n", "");
Upvotes: 3