【发布时间】:2019-04-18 13:42:29
【问题描述】:
当我试图从我的 java 代码中调用它时,我正面临来自 https REST 服务的 HTTP 403 Forbidden 响应。如果我在这里遗漏了什么,您能告诉我吗?
请注意,当我从任何浏览器/SOAPUI/Chrome Postman 客户端触发请求时,服务器会返回预期的数据。 使用了 2 个对等证书 - 如发送请求后来自 soapui 的 ssl 信息所示。
附上代码sn-p。 [我在代码中设置的headers取自我从成功请求中找到的请求头]
HttpsURLConnection connection = (HttpsURLConnection)new URL("https://server address").openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding","gzip, deflate, br");
connection.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1");
connection.addRequestProperty("Connection", "Keep-Alive");
connection.addRequestProperty("Cache-Control","no-cache");
connection.setRequestMethod("GET");
connection.connect();
System.out.println("Response Code : " + connection.getResponseCode()+" "+connection.getResponseMessage());
响应代码:403 禁止
【问题讨论】:
-
收到
403 Forbidden后,可能是您请求的网址不正确。可能是其余服务的日志文件显示了更多信息。可以发一下吗?