简体   繁体   English

RESTFUL码头服务返回403错误Java

[英]RESTFUL jetty service returning 403 error java

I am facing this HTTP 403 Forbidden response from a https REST service when I am trying to call it from my java code. 当我尝试从我的Java代码调用它时,我面临来自https REST服务的HTTP 403 Forbidden响应。 Can you kindly let me know if I am missing something here? 您能告诉我我是否在这里遗漏了什么吗?

Please note that the server returns the expected data when I trigger the request from any browser / SOAPUI/ Chrome Postman clients. 请注意,当我从任何浏览器/ SOAPUI / Chrome Postman客户端触发请求时,服务器将返回预期数据。 2 peer certificates are used - as shown in the ssl info from soapui after the request is sent. 使用2个对等证书-如发送请求后来自soapui的ssl信息所示。

The code snippet is attached. 该代码段已附加。 [The headers I set in the code are taken from the request header I found from the successful requests] [我在代码中设置的标头取自从成功请求中找到的请求标头]

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());

Response Code : 403 Forbidden 响应码:403 Forbidden

Can you please check the Server URL if it is in the Java Acceptable format? 您可以检查服务器URL是否为Java可接受格式吗?
Sometimes java need escape characters to recognize strings correctly. 有时Java需要转义字符才能正确识别字符串。 This question: What are all the escape characters? 这个问题: 所有转义字符是什么? , can help you to check if you are using any of those characters. ,可以帮助您检查是否正在使用这些字符中的任何一个。 Also check if the conversion in the function is done properly. 还要检查函数中的转换是否正确完成。
Also, if you have more complex URL, consider to use java.net.URL . 另外,如果您具有更复杂的URL,请考虑使用java.net.URL。
Finally, check the user agent parameter Setting user agent of a java URLConnection . 最后,检查用户代理参数“ 设置Java URLConnection的用户代理”

Thanks for your response. 感谢您的答复。 The issue is with session cookie to be used for the connection. 问题在于会话cookie用于连接。 We are able to connect and get the response back with response code HTTP 200 once the cookie with JSESSIONID is passed as a header. 一旦带有JSESSIONID的cookie作为标头传递,我们就可以连接并使用响应代码HTTP 200获取响应。 Thanks again. 再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM