Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http2Client communicate with third party server #233

Open
HarryXQ opened this issue Sep 4, 2022 · 2 comments
Open

Http2Client communicate with third party server #233

HarryXQ opened this issue Sep 4, 2022 · 2 comments

Comments

@HarryXQ
Copy link

HarryXQ commented Sep 4, 2022

How Http2Client communicate to third party server Url:

"https://api.weixin.qq.com/sns/jscode2session?appid=AppID&secret=AppSecret"&js_code=Code&grant_type=authorization_code"

and get the response body.

@HarryXQ
Copy link
Author

HarryXQ commented Sep 4, 2022

My Code:

ClientConnection connect = null;
try {
String code = requestEntity.getPathParameters().get("code").getFirst();
URL url = new URL("https://api.weixin.qq.com/sns/jscode2session?appid="+AppID+"&secret="+AppSecret+"&js_code="+code+"&grant_type=authorization_code");

        Http2Client instance = Http2Client.getInstance();
        connect = instance.connect(url.toURI(), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();

        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<ClientResponse> reference = new AtomicReference<>();
        ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(url.getPath());
        request.getRequestHeaders().add(new HttpString("appid"), AppID);
        request.getRequestHeaders().add(new HttpString("secret"), AppSecret);
        request.getRequestHeaders().add(new HttpString("js_code"), code);
        request.getRequestHeaders().add(new HttpString("grant_type"), "authorization_code");
        request.getRequestHeaders().put(Headers.HOST, "localhost");
        connect.sendRequest(request, instance.createClientCallback(reference, latch));
        latch.await(1000, TimeUnit.MILLISECONDS);

        int statusCode = reference.get().getResponseCode();
        if (statusCode >= 300) {
            throw new Exception("Failed to call API jscode2session: " + statusCode);
        }
        String jsObj = Config.getInstance().getMapper().readValue(reference.get().getAttachment(Http2Client.RESPONSE_BODY), new TypeReference<>() {});

        HeaderMap responseHeaders = new HeaderMap();
        responseHeaders.add(Headers.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        return new ResponseEntity<>(jsObj, responseHeaders, HttpStatus.OK);
    } catch (Exception e) {
        Helper.loggerError(e.getMessage());
    } finally {
        IoUtils.safeClose(connect);
    }

Log: Cannot invoke "io.undertow.client.ClientResponse.getResponseCode()" because the return value of "java.util.concurrent.atomic.AtomicReference.get()" is null

@stevehu
Copy link
Contributor

stevehu commented Sep 7, 2022

You might need to increase the timeout when connecting to a third-party server. Also, you have to add the third-party server certificate to the client.truststore since you are using HTTPS.

latch.await(1000, TimeUnit.MILLISECONDS);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants