Java 带有身份验证的 Jersey HTTPS GET 请求 header
Java Jersey HTTPS GET request with Authentication header
我正在尝试将通过 HTTPS 请求工作的支付服务 'Mollie' (http://www.mollie.nl) 集成到 Java 环境中。
至于这篇文章,我将使用以下请求来解释:
在 PHP 内(因为我有 PHP 背景)我可以使用 cURL:
$ curl -X GET https://api.mollie.nl/v1/methods \
-H "Authorization: Bearer API-KEY"
其中有回复:
正在测试来自 DHC(或 Postman)的请求 return 正确响应。
所以在 Java 我正在使用 Jersey 库尝试访问请求:
Client client = Client.create();
WebResource webResource = client.resource("https://api.mollie.nl/v1/methods");
webResource.header("Authorization", "Bearer API-KEY");
ClientResponse response = webResource
.header("Content-Type", "application/json;charset=UTF-8")
.type("application/json")
.accept("application/json")
.get(ClientResponse.class);
int statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or Password");
}
String responseCall = response.getEntity(String.class);
执行 Java 代码时,请求抛出 ClientHandlerException:
HTTP Status 500 - com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection timed out: connect
我是 运行 来自 Apache 本地主机服务器的 Java 测试。
但我不明白为什么 Java 请求会超时,因为身份验证 header 似乎设置正确(至少对我而言)。
我注意到的是在访问请求路径时 https://api.mollie.nl/v1/methods 它显示了一个 pop-up 用于身份验证。
如果能获得有关此问题的一些有用的提示或信息,那就太好了。
我错过了什么吗?
谢谢!
假设一切正常(我不确定为什么会导致超时),我认为错误的一件事是您对
的使用
webResource.header("Authorization", "Bearer API-KEY");
header
returns an WebResource.Builder
,并且 不会 将 header 添加到当前的 WebResource
。所以您发送的请求没有 header。您可以通过添加 LoggingFilter
来检查它
client.addFilter(new com.sun.jersey.api.client.filter.LoggingFilter(System.out));
您可以通过
解决这个问题
ClientResponse response = webResource
.header("Authorization", "Bearer API-KEY");
.header("Content-Type", "application/json;charset=UTF-8")
只需将 header 移动到方法链。
我已经解决了上面的问题。显然,工作代理服务器正在阻止传出的 HTTPS 请求。从非代理环境进行测试解决了这个问题。谢谢大家的建议!
我正在尝试将通过 HTTPS 请求工作的支付服务 'Mollie' (http://www.mollie.nl) 集成到 Java 环境中。
至于这篇文章,我将使用以下请求来解释: 在 PHP 内(因为我有 PHP 背景)我可以使用 cURL:
$ curl -X GET https://api.mollie.nl/v1/methods \
-H "Authorization: Bearer API-KEY"
其中有回复:
正在测试来自 DHC(或 Postman)的请求 return 正确响应。
所以在 Java 我正在使用 Jersey 库尝试访问请求:
Client client = Client.create();
WebResource webResource = client.resource("https://api.mollie.nl/v1/methods");
webResource.header("Authorization", "Bearer API-KEY");
ClientResponse response = webResource
.header("Content-Type", "application/json;charset=UTF-8")
.type("application/json")
.accept("application/json")
.get(ClientResponse.class);
int statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or Password");
}
String responseCall = response.getEntity(String.class);
执行 Java 代码时,请求抛出 ClientHandlerException:
HTTP Status 500 - com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection timed out: connect
我是 运行 来自 Apache 本地主机服务器的 Java 测试。 但我不明白为什么 Java 请求会超时,因为身份验证 header 似乎设置正确(至少对我而言)。
我注意到的是在访问请求路径时 https://api.mollie.nl/v1/methods 它显示了一个 pop-up 用于身份验证。
如果能获得有关此问题的一些有用的提示或信息,那就太好了。 我错过了什么吗?
谢谢!
假设一切正常(我不确定为什么会导致超时),我认为错误的一件事是您对
的使用webResource.header("Authorization", "Bearer API-KEY");
header
returns an WebResource.Builder
,并且 不会 将 header 添加到当前的 WebResource
。所以您发送的请求没有 header。您可以通过添加 LoggingFilter
client.addFilter(new com.sun.jersey.api.client.filter.LoggingFilter(System.out));
您可以通过
解决这个问题ClientResponse response = webResource
.header("Authorization", "Bearer API-KEY");
.header("Content-Type", "application/json;charset=UTF-8")
只需将 header 移动到方法链。
我已经解决了上面的问题。显然,工作代理服务器正在阻止传出的 HTTPS 请求。从非代理环境进行测试解决了这个问题。谢谢大家的建议!