简体   繁体   English

带有Authentication头的Java Jersey HTTPS GET请求

[英]Java Jersey HTTPS GET request with Authentication header

I'm trying to integrate a payment service 'Mollie' ( http://www.mollie.nl ) which works over HTTPS requests into a Java environment. 我正在尝试将支持服务的“Mollie”( http://www.mollie.nl )集成到Java环境中,该服务通过HTTPS请求工作。

As for this posts i'll be using following request to explain: Within PHP (since I have a PHP background) I can work with cURL: 至于这篇文章,我将使用以下请求来解释:在PHP中(因为我有PHP背景)我可以使用cURL:

$ curl -X GET https://api.mollie.nl/v1/methods \
-H "Authorization: Bearer API-KEY"

Which has a response: 哪有回应:

在此输入图像描述

Testing the REQUEST from DHC (or Postman) return correct response. 从DHC(或Postman)测试REQUEST返回正确的响应。

在此输入图像描述

So within Java i'm using the Jersey library to try to access the Request: 所以在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);

When executing the Java code the request throws a ClientHandlerException: 执行Java代码时,请求会抛出ClientHandlerException:

HTTP Status 500 - com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection timed out: connect

I'm running the Java test from a Apache localhost server. 我正在从Apache localhost服务器运行Java测试。 But I can't figure out why the Java request gives a timeout since the authentication header seems to be set correct (at least to me). 但我无法弄清楚为什么Java请求会给出超时,因为身份验证标头似乎设置正确(至少对我而言)。

What I did notice is when visiting the path of the request https://api.mollie.nl/v1/methods it shows a pop-up for authentication. 我注意到的是访问请求的路径时https://api.mollie.nl/v1/methods它显示了一个用于身份验证的弹出窗口。

It would be nice to get some usefull tips or information about this issue. 获得有关此问题的有用提示或信息会很高兴。 Am I missing something? 我错过了什么吗?

Thanks! 谢谢!

Given all is is working correctly (I'm not sure why it would cause a timeout), one thing I see wrong is your usage of 鉴于所有的工作正常(我不确定为什么会导致超时),我看错了一件事是你的用法

webResource.header("Authorization", "Bearer API-KEY");

header returns an WebResource.Builder , and does not add the header to the current WebResource . header返回WebResource.Builder ,并且不会将标头添加到当前WebResource So the request you are sending doesn't have the header. 因此,您发送的请求没有标头。 You can check it by adding a LoggingFilter 您可以通过添加LoggingFilter来检查它

client.addFilter(new com.sun.jersey.api.client.filter.LoggingFilter(System.out));

You can fix this by doing 你可以通过这样做解决这个问题

ClientResponse response = webResource
        .header("Authorization", "Bearer API-KEY");    
        .header("Content-Type", "application/json;charset=UTF-8")

Just moving the header to the method chaining. 只需将标题移动到方法链接。

I have resolved above issue. 我已经解决了上述问题。 Apparently the work PROXY server was blocking outgoing HTTPS requests. 显然工作PROXY服务器阻止了传出的HTTPS请求。 Testing from a non-proxy environment fixed the issue. 从非代理环境进行测试可以解决问题。 Thanks for all advice! 谢谢你的建议!

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

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