如何在Java中发布请求以在JSON中获取响应

发布于 2025-01-30 17:44:15 字数 1736 浏览 3 评论 0原文

我想以同一构造发布该请求。

    POST /mga/sps/oauth/oauth20/token HTTP/1.1
    Host: example.com
    Content-Type: application/x-www-form-urlencoded
    Authorization: Basic aaabbbCCCdddeeefffGGG
    client_id=xxx&client_secret=yyy&grant_type=authorization_code
    &code=3v6MJzt9vKtRkxpTFnkJG3IyspWC2k
    &redirect_uri=xyz%2Ffolder

我已经实施了,但得到了不良的请求,无法打印帖子内容,我在发送此请求后也想获得JSON响应。

String urlParameters  = "grant_type=authorization_code"+"&redirect_uri="+session.getAttribute("redirect_uri")+"&code_verifier="+session.getAttribute("codeVerifier")+"&code="+session.getAttribute("code")+"&state="+session.getAttribute("state");
        byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
        int postDataLength = postData.length;
        URL url = new URL( "https://example/oauth20/token" );
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
        conn.setDoOutput(true);
        conn.setInstanceFollowRedirects(false);
        conn.setRequestMethod("POST");
     
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        conn.setRequestProperty("charset", "utf-8");
        conn.setRequestProperty("Content-Length", 
       Integer.toString(postDataLength ));
        conn.setRequestProperty("Host","example.com");
        conn.setRequestProperty("Authorization","clientID=xyz");
        conn.setUseCaches(false);
        DataOutputStream wr = new 
       DataOutputStream(conn.getOutputStream());
        wr.write(postData);
        System.out.println(conn.getResponseCode());
        System.out.println(conn.getResponseMessage());
        conn.disconnect();

 

I want to post the request in same formate.

    POST /mga/sps/oauth/oauth20/token HTTP/1.1
    Host: example.com
    Content-Type: application/x-www-form-urlencoded
    Authorization: Basic aaabbbCCCdddeeefffGGG
    client_id=xxx&client_secret=yyy&grant_type=authorization_code
    &code=3v6MJzt9vKtRkxpTFnkJG3IyspWC2k
    &redirect_uri=xyz%2Ffolder

I have Implemented but getting bad request and unable to print the post content what I am sending I also want to get the json response after sending this request.

String urlParameters  = "grant_type=authorization_code"+"&redirect_uri="+session.getAttribute("redirect_uri")+"&code_verifier="+session.getAttribute("codeVerifier")+"&code="+session.getAttribute("code")+"&state="+session.getAttribute("state");
        byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
        int postDataLength = postData.length;
        URL url = new URL( "https://example/oauth20/token" );
        HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
        conn.setDoOutput(true);
        conn.setInstanceFollowRedirects(false);
        conn.setRequestMethod("POST");
     
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        conn.setRequestProperty("charset", "utf-8");
        conn.setRequestProperty("Content-Length", 
       Integer.toString(postDataLength ));
        conn.setRequestProperty("Host","example.com");
        conn.setRequestProperty("Authorization","clientID=xyz");
        conn.setUseCaches(false);
        DataOutputStream wr = new 
       DataOutputStream(conn.getOutputStream());
        wr.write(postData);
        System.out.println(conn.getResponseCode());
        System.out.println(conn.getResponseMessage());
        conn.disconnect();

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2025-02-06 17:44:15

您有多个选项。

  1. 您可以从 java http client -

在Java 11中添加了HTTP客户端。它可用于请求HTTP
通过网络的资源。它支持HTTP/1.1和HTTP/2
同步和异步编程模型,处理请求和
响应体作为反应性流,并遵循熟悉的建筑商
模式。

  1. apache httpclient -

  2. > RESTTEMPLATE - 参考

  3. jax-rs client - 示例

  4. Spring 5 webclient - 示例

  5. okhttpclient - 示例

比较

You have multiple options.

  1. You can start with Java HTTP Client - Refer

The HTTP Client was added in Java 11. It can be used to request HTTP
resources over the network. It supports HTTP/1.1 and HTTP/2, both
synchronous and asynchronous programming models, handles request and
response bodies as reactive-streams, and follows the familiar builder
pattern.

  1. Apache HttpClient - Refer

  2. RestTemplate - Refer

  3. JAX-RS Client - Example

  4. Spring 5 WebClient - Example

  5. OkHttpClient - Example

Comparison

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文