3

我正在尝试使用 Java 中的 Rest api 访问 QC 11。

我正在遵循 HP 提供的手册中的 API 参考。以下是登录认证的基本步骤。

非 Web 应用程序授权客户端查询经过身份验证的资源并且不发送任何身份验证标头。此步骤是可选的。

GET /qcbin/rest/is-authenticated

服务器拒绝请求并返回对认证点的引用。

HTTP/1.1 401 未经授权的 WWW-Authenticate: LWSSO realm=http://[server]:[port]/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 来自服务器的错误请求输出 ....

我是使用 Java 的 REST 新手。任何人都可以帮忙吗?任何使用 REST 连接到 ALM 并获取数据的示例?

4

1 回答 1

3

解决了这个问题。

问题出在base64编码中!如果要将字符串转换为 base64 编码的字符串...如果结果大于 76 个字符。它增加了新的一行!即使它小于 76

所以解决方案是

encoding = encoding.replaceAll("\n", "");
于 2012-06-08T06:00:36.167 回答