我正在尝试HP ALM 12,发现REST api已更改,但是我不知道如何使它与在ALM 11.5上正常工作的Java代码一起使用。
使用ALM 11.5时,我发送以下http请求:
http://myALM:8000/qcbin/rest/is-authenticated (which calls /authenticate)
http://myALM:8000/qcbin/rest/domains/DEFAULT/projects/DEMO/requirements?login-form-required=y&fields=father-name,name,req-priority,request-status,description,name,status&query={status[NOT%20N/A]}
上面的一切都很好。
使用ALM 12时,其REST文档说我需要显式调用/ rest / site-session资源以获得QCSession(在第一次调用任何资源时ALM 11.5中会自动返回该QCSession),但是我无法获得对返回QCSession。以下是为了发送到ALM 12而发送的http请求的列表:
http://myALM:8000/qcbin/rest/is-authenticated (which calls authenticate)
http://myALM:8000/qcbin/rest/site-session
http://myALM:8000/qcbin/rest/domains/DEFAULT/projects/DEMO/requirements?login-form-required=y&fields=father-name,name,req-priority,request-status,description,name,status&query={status[NOT%20N/A]}
对rest / site-session的调用返回JSESSIONID,就好像它是第一个请求一样,显然其余代码都失败了。
我在这里做错了什么?
最佳答案
您需要POST
并保存来自响应的Cookie到RestConnector类。
我添加到类中,它作为RestConnector上方的“薄”层
public void GetQCSession(){
String qcsessionurl = restConnector.buildUrl("rest/site-session");
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Content-Type", "application/xml");
requestHeaders.put("Accept", "application/xml");
try {
Response resp = restConnector.httpPost(qcsessionurl, null, requestHeaders);
restConnector.updateCookies(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
RestConnector
updateCookies
方法public void updateCookies(Response response) {
Iterable<String> newCookies =
response.getResponseHeaders().get("Set-Cookie");
if (newCookies != null) {
for (String cookie : newCookies) {
int equalIndex = cookie.indexOf('=');
int semicolonIndex = cookie.indexOf(';');
String cookieKey = cookie.substring(0, equalIndex);
String cookieValue =
cookie.substring(equalIndex + 1, semicolonIndex);
cookies.put(cookieKey, cookieValue);
}
}
}