简体   繁体   English

HttpsURLConnection-发送POST请求

[英]HttpsURLConnection - Send POST request

I want to send a POST request to this particular API: https://developer.lufthansa.com/docs/read/api_basics/Getting_Started and I researched how to do that and tried everything but it simply doesn't work, I always get an HTTP 400 or an HTTP 401 error. 我想向该特定的API发送POST请求: https : //developer.lufthansa.com/docs/read/api_basics/Getting_Started ,我研究了如何执行此操作并尝试了所有方法,但它根本无法正常工作,我总是能得到HTTP 400或HTTP 401错误。 Here's my code: 这是我的代码:

private void setAccessToken(String clientID, String clientSecret) {
    try {
        URL url = new URL(URL_BASE + "oauth/token");
        String params = "client_id=" + clientID + "&client_secret=" + clientSecret + "&grant_type=client_credentials";
        HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.connect();
        OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
        osw.write(params);
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
}

Kenta1561 Kenta1561

Seems that your code is working well and it may be the case that you are providing invalid clientID or clientSecret so that your are getting wrong response in this case (as 401 indicates unauthorized). 似乎您的代码运行良好,并且可能是由于您提供了无效的clientID或clientSecret,从而使您在这种情况下得到了错误的响应(如401表示未授权)。 One thing you can do is you are only getting the response message if the http request status is ok (200). 您可以做的一件事是,仅在http请求状态正常时才收到响应消息(200)。 You may also get the invalid response message in case of 400 or 401 http response status. 在400或401 http响应状态的情况下,您也可能会收到无效的响应消息。 In order to print the invalid response messages you may follow the code below: 为了打印无效的响应消息,您可以遵循以下代码:

private void setAccessToken(String clientID, String clientSecret) throws Exception {

    String params = "client_id=" + clientID + "&client_secret=" + clientSecret + "&grant_type=client_credentials";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    BufferedReader in;
    // add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(params);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    if (responseCode >= 400)
        in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
    else 
        in = new BufferedReader(new InputStreamReader(con.getInputStream()));

    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    System.out.println(response.toString());
}

In this way you can also get invalid response message. 这样您还可以获得无效的响应消息。 In your case when I tried to hit the provided api it is giving me the response below: 在您的情况下,当我尝试点击提供的api时,会给我以下响应:

{"error": "invalid_client"}

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

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