【问题标题】:How to convert this curl command into java command如何将此 curl 命令转换为 java 命令
【发布时间】:2021-10-08 13:27:54
【问题描述】:

我需要把下面的curl命令转换成java命令。

curl https://api.linkedin.com/v2/me -H "Authorization: Bearer xxx"

我写了这段代码:

public static void main(String[] args) throws MalformedURLException, IOException{

    HttpURLConnection con = (HttpURLConnection) new URL("https://api.linkedin.com/v2/me").openConnection();

    con.setRequestMethod("POST");
    con.setRequestProperty("Authorization", "Bearer xxx");
    con.setDoOutput(true);
    con.setDoInput(true);
    con.getOutputStream().write("LOGIN".getBytes("UTF-8"));
    con.getInputStream();
}

但我得到一个错误:

Exception in thread "main" java.io.FileNotFoundException: https://api.linkedin.com/v2/me
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1915)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
    at testiamo.main(testiamo.java:18)

【问题讨论】:

  • 试试HttpsURLConnection con = (HttpsURLConnection)new URL("https://api.linkedin.com/v2/me").openConnection(); (import javax.net.ssl.HttpsURLConnection;)

标签: java curl


【解决方案1】:

我会选择Apache HttpClient

并以这种方式执行调用:

        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpPost postRequest = new HttpPost("https://api.linkedin.com/v2/me");
        postRequest.addHeader("Authorization", "Bearer xxx");

        // You could also send something through JSON...
        if(requestBody != null)
        {
            postRequest.addHeader("content-type", "application/json");
            StringEntity jsonEntity = new StringEntity(requestBody);
            postRequest.setEntity(jsonEntity);
        }

        // Getting the response
        HttpResponse rawResponse = client.execute(postRequest);
        final int status = rawResponse.getStatusLine().getStatusCode();
        final StringBuilder response = new StringBuilder();

        // If status == 200 then we get the response (which could be JSON, XML and so on) and save it as a string.
        if(status == HttpStatus.SC_OK)
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    rawResponse.getEntity().getContent()));

            String inputLine;

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

            reader.close();
            client.close();
        }
        else
        {
            client.close();
            throw new RuntimeException("Error while doing call!\nStatus: "+ status);
        }

更多信息,请点击here

【讨论】:

  • 我收到此错误:2021 年 4 月 4 日上午 10:30:17 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告:cookie 标头无效:“Set-Cookie: li_gc=MTswOzE2MjgwNjU4MTc7MjswMjFHaojFfu7ZrccbD2V9NU7pLUJlh my7cV7lmf3L/aRg==;域=.linkedin.com;过期=格林威治标准时间 2023 年 7 月 21 日星期五 00:02:44;路径=/;安全;SameSite=无”。无效的“过期”属性:2023 年 7 月 21 日星期五 00:02:44 GMT 线程“main”java.lang.RuntimeException 中的异常:调用时出错!状态:testiamo.main(testiamo.java:47) 的 401 进程以退出代码完成
  • 自从调用执行以来,上面提出的问题已经得到解答,您会收到 HTTP STATUS 401 作为响应。该错误代码意味着您未经授权。如果您需要有关此错误的帮助,我建议您提出另一个具体问题并将所有详细信息放在那里。我很乐意提供帮助。 httpstatuses.com/401
  • 谢谢,我刚刚又问了一个问题:stackoverflow.com/questions/68648697/…
猜你喜欢
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多