我正在尝试使用Java中的Rest api访问QC 11.
我正在遵循HP提供的手册中的API参考.
以下是登录身份验证的基本步骤.
Non-Web Application Authorization Client queries the is-authenticated resource and sends no authentication headers. This
step is optional.
GET / qcbin / rest /已验证
服务器拒绝请求,并返回对身份验证点的引用.
HTTP / 1.1 401未经授权的WWW身份验证:LWSSO
realm = http:// [服务器]:[端口] / qcbin / authentication-point
客户端将有效的基本身份验证标头发送到身份验证
点.
GET / qcbin / authentication-point / authenticate授权:基本
ABCDE123
服务器验证基本身份验证标头,创建一个新的
LW-SSO令牌,并将其作为LWSSO_COOKIE_KEY返回.
HTTP / 1.1 200 OK设置cookie:LWSSO_COOKIE_KEY = {cookie}
应用程序现在可以使用令牌访问数据和服务.在
在会话结束时,注销以放弃令牌.这是我的Java代码.
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();
它给我输出为
[Lorg.apache.http.Header;@159e154 HTTP/1.1 400 Bad Request Output from
Server ….我是使用Java的REST新手.有人可以帮忙吗?
使用REST连接到ALM并获取数据的任何示例吗?
解决方法:
解决了问题.
问题出在base64编码中!
如果要将字符串转换为base64编码的字符串,则结果大于76个字符.它增加了新的一行!
即使少于76
所以解决方案是
encoding = encoding.replaceAll("\n", "");