gpt4 book ai didi

java - 从java Rest API调用rest API

转载 作者:行者123 更新时间:2023-11-30 02:55:57 24 4
gpt4 key购买 nike

我在 JBoss 上托管了一个 Java Rest API,它调用另一个 Rest Web 服务:

@GET
@Path("/testDeployment")
@Produces(MediaType.TEXT_PLAIN)
public String testDeployment() {
URL url = new URL(restURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer "+sessionId);

System.out.println("sessionId>>>> "+sessionId);
System.out.println("restURL>>>> "+restURL);
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
response += output;
}

conn.disconnect();
}

但是我收到错误

服务器返回 HTTP 响应代码:401,URL:https://cs5.salesforce.com/services/apexrest/event/search?type=init

13:16:08,738 错误 [stderr](默认任务 26)java.io.IOException:服务器返回 HTTP 响应代码:401 对于 URL:https://cs5.salesforce.com/services/apexrest/event/search?type=init

13:16:08,747 错误 [stderr](默认任务 26)位于 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)

最佳答案

以下是 Http Status Code Definitions 定义的摘录,这可能会帮助您解决问题:

401 Unauthorized

The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication"

关于java - 从java Rest API调用rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204205/

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