首页 > 解决方案 > 带有正文 x-www-urlencode 授权类型、用户、密码的 Java Post 请求

问题描述

你好我试图做这样的请求:

public Object getToken()  throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://URL_SERVEUR_AUTH.com");
    httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("grant_type", "client_credentials"));
    params.add(new BasicNameValuePair("user", "USER"));
    params.add(new BasicNameValuePair("password", "PASSWORD"));
    httpPost.setEntity(new UrlEncodedFormEntity(params));
    System.out.println(httpPost);

    CloseableHttpResponse response = client.execute(httpPost);
    client.close();
    return response;
}

看答案:

HttpResponseProxy{HTTP/1.1 500 ERROR [Date: Mon, 08 Oct 2018 09:30:05 GMT, 
Connection: close,
Host: http://URL_SERVEUR_AUTH.com,
Content-Type: text/html; charset="utf-8",
Set-Cookie: TS****=***********************************; Path=/,
Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: text/html; 
charset="utf-8",Chunked: true]}}

标签: javahttppost

解决方案


推荐阅读