gpt4 book ai didi

java - HP ALM Rest API QCSession 411 身份验证

转载 作者:可可西里 更新时间:2023-11-01 16:32:11 26 4
gpt4 key购买 nike

我正在使用 HP-ALM 12.01,它似乎充满了问题。我现在无法更新到另一个版本。

我正在尝试访问其余 API 以自动从 JUnit 上传测试结果。我正在使用显示的基础设施 here (示例应用程序 -> 基础设施)。从中,我的连接脚本将 base64 编码的登录信息传递给身份验证点/身份验证,我正在检索有效的 LWSSO cookie。但是,当我使用此 cookie 连接到 rest/site-session 以接收我的 QCSession cookie 时,我收到了 411 Length Required 错误。我已尝试将 Content-Length 硬编码到 header 中,如此处所示

public void GetQCSession(){
String qcsessionurl = con.buildUrl("rest/site-session");
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Content-Type", "application/xml");
requestHeaders.put("Accept", "application/xml");
requestHeaders.put("Content-Length", "0");
try {
Response resp = con.httpPost(qcsessionurl, null, requestHeaders);
con.updateCookies(resp);
System.out.println(resp.getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}

这没有用。我还尝试修改基础架构以自动注入(inject) Content-Length header ,如下所示

    private void prepareHttpRequest(
HttpURLConnection con,
Map<String, String> headers,
byte[] bytes,
String cookieString) throws IOException {

String contentType = null;

//attach cookie information if such exists
if ((cookieString != null) && !cookieString.isEmpty()) {

con.setRequestProperty("Cookie", cookieString);
}

//send data from headers
if (headers != null) {

//Skip the content-type header - should only be sent
//if you actually have any content to send. see below.
contentType = headers.remove("Content-Type");

Iterator<Entry<String, String>>
headersIterator = headers.entrySet().iterator();
while (headersIterator.hasNext()) {
Entry<String, String> header = headersIterator.next();
con.setRequestProperty(header.getKey(), header.getValue());
}
}

// If there's data to attach to the request, it's handled here.
// Note that if data exists, we take into account previously removed
// content-type.
if ((bytes != null) && (bytes.length > 0)) {

con.setDoOutput(true);

//warning: if you add content-type header then you MUST send
// information or receive error.
//so only do so if you're writing information...
if (contentType != null) {
con.setRequestProperty("Content-Type", contentType);
}

OutputStream out = con.getOutputStream();
out.write(bytes);
out.flush();
out.close();
con.setRequestProperty("Content-Length", Integer.toString(bytes.length));
} else {
con.setRequestProperty("Content-Length", "0");
}
}

这也行不通。请注意,setRequestProperty 只是对 MessageHeader 执行 .set(key, value)

有没有人处理过这个问题或者知道如何解决它?

请注意, postman 不会出现这些问题。所有 4 个 cookie 都是在站点 session 发布后生成的。

最佳答案

Barney 的代码示例略有扩展,因为它不适用于 ALM 12.5 设置。主要区别在于,有更多的 cookie,并且 cookie 附加到 header

  Config config = new Config(dataService);
String almURL = "https://" + config.host() + "/qcbin";

client = ClientBuilder.newBuilder().build();
target = client.target(almURL).path("api/authentication/sign-in");
invocationBuilder = target
.request(new String[] {"application/xml"})
.accept(new String[] {"application/xml"});
invocationBuilder.header("Authorization", getEncodedAuthString(config.username(), config.password()));

res = invocationBuilder.post(null);

String qcsessioncookie = res.getCookies().get("QCSession").getValue();
String almusercookie = res.getCookies().get("ALM_USER").getValue();
String xsrftokencookie = res.getCookies().get("XSRF-TOKEN").getValue();
String lswoocookie = res.getCookies().get("LWSSO_COOKIE_KEY").getValue();

/* Get the test-Set Data defect */
String midPoint = "rest/domains/" + config.domain() + "/projects/" + config.project();
target = client.target(almURL).path(midPoint).path("test-sets/1");
invocationBuilder = target
.request(new String[] {"application/xml"})
.accept(new String[] {"application/xml"});

concatenatedHeaderCookieString = "QCSession=" + qcsessioncookie + ";" + "ALM_USER=" + ";" + almusercookie + ";" + "XSRF-TOKEN=" + xsrftokencookie + ";"
+ "LWSSO_COOKIE_KEY=" + lswoocookie;
invocationBuilder.header("Cookie", concatenatedHeaderCookieString);

res = invocationBuilder.get();

关于java - HP ALM Rest API QCSession 411 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45152242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com