gpt4 book ai didi

java客户端程序使用HttpClient API发送摘要认证请求

转载 作者:行者123 更新时间:2023-12-01 09:15:34 26 4
gpt4 key购买 nike

我有发送摘要请求的 reSTLet 示例客户端程序。与此类似,我需要使用 HttpClient api 发送摘要请求的 java 客户端程序。谁能给我发送示例代码。提前致谢。

        Reference reference = new Reference("http://localhost:8092/authenticate");
Client client = new Client(Protocol.HTTP);
Request request = new Request(Method.GET, reference);
Response response = client.handle(request);
System.out.println("response: "+response.getStatus());

Form form = new Form();
form.add("username", "rajesh");
form.add("uri", reference.getPath());

// Loop over the challengeRequest objects sent by the server.
for (ChallengeRequest challengeRequest : response
.getChallengeRequests()) {
// Get the data from the server's response.
if (ChallengeScheme.HTTP_DIGEST
.equals(challengeRequest.getScheme())) {
Series<Parameter> params = challengeRequest.getParameters();
form.add(params.getFirst("nonce"));
form.add(params.getFirst("realm"));
form.add(params.getFirst("domain"));
form.add(params.getFirst("algorithm"));
form.add(params.getFirst("qop"));
}
}

// Compute the required data
String a1 = Engine.getInstance().toMd5(
"rajesh" + ":" + form.getFirstValue("realm") + ":" + "rajesh");
String a2 = Engine.getInstance().toMd5(
request.getMethod() + ":" + form.getFirstValue("uri"));
form.add("response", Engine.getInstance().toMd5(
a1 + ":" + form.getFirstValue("nonce") + ":" + a2));

ChallengeResponse challengeResponse = new ChallengeResponse(
ChallengeScheme.HTTP_DIGEST, "", "");
challengeResponse.setCredentialComponents(form);

// Send the completed request
request.setChallengeResponse(challengeResponse);
response = client.handle(request);

// Should be 200.
System.out.println(response.getStatus());

最佳答案

您是否尝试过以下方法:

ChallengeResponse challengeResponse = new ChallengeResponse(challengeRequest, "rajesh", <password>);

关于java客户端程序使用HttpClient API发送摘要认证请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2152573/

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