java - Apache HttpClient 4.x 不支持的媒体类型

标签 java cookies apache-httpclient-4.x

我一直在尝试从 HTTP POST 请求获取 cookie,该 cookie 用于使用用户名和密码在特定 REST API 上进行身份验证。问题在于cookie存储中没有cookie(所有相关的隐藏参数都是正确的)。用于验证用户身份的 POST 方法的主体 (JSON) 是:

{
  "username": <username>,
  "password": <password>
}

我正在使用以下代码:

    public static void main(String[] args) {

    String USER_AUTHENTICATION = "/user/authentication";
    String baseUrl = "http://<someIP>/<someProjectName>/rest";

    HttpClient http = null;
    CookieStore httpCookieStore = new BasicCookieStore();
    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("<someUsername>", "<somePassword>"));

    HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore)
            .setDefaultRequestConfig(globalConfig).setDefaultCredentialsProvider(credentialsProvider);
    http = builder.build();

    HttpPost httpRequest = new HttpPost(baseUrl + USER_AUTHENTICATION);
    HttpResponse httpResponse;
    try {
        httpResponse = http.execute(httpRequest);
    } catch (Throwable error) {
        throw new RuntimeException(error);
    }

    List<Cookie> cookies = httpCookieStore.getCookies();
    System.out.println("Cookies! " + cookies);
}

控制台输出为:

12:09:34.267 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: standard
12:09:34.295 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
12:09:34.298 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://<someIP>][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
12:09:34.343 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://<someIP>][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
12:09:34.346 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {}->http://<someIP>
12:09:34.355 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to /<someIP>
12:09:34.363 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connection established <someOtherIP>:<somePort><-><someIP>
12:09:34.363 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Executing request POST /<someProjectName>/rest/user/authentication HTTP/1.1
12:09:34.363 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
12:09:34.365 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
12:09:34.368 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /<someProjectName>/rest/user/authentication HTTP/1.1
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 0
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: <someIP>
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_91)
12:09:34.369 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "POST /<someProjectName>/rest/user/authentication HTTP/1.1[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 0[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: <someIP>[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
12:09:34.369 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_91)[\r][\n]"
12:09:34.370 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
12:09:34.370 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 415 Unsupported Media Type[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Tue, 10 Jan 2017 11:09:39 GMT[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: Apache-Coyote/1.1[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 0[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Access-Control-Allow-Origin: *[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: close[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: text/plain; charset=UTF-8[\r][\n]"
12:09:34.406 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 415 Unsupported Media Type
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Tue, 10 Jan 2017 11:09:39 GMT
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 0
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Access-Control-Allow-Origin: *
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: close
12:09:34.413 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: text/plain; charset=UTF-8
12:09:34.419 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
12:09:34.420 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
12:09:34.420 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://<someIP>][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
Cookies! []

我的想法:可能是媒体类型不受支持的问题,可以在控制台输出中看到,但我不知道如何解决:(

编辑:我收到了添加有关应用程序的附加信息的建议,所以这里是:任务是获取一个 cookie,该 cookie 将在对 REST API 的各种请求中传递。为此,我首先必须使用用户名和密码进行身份验证(POST),该用户名和密码将提供 cookie 本身,然后我应该检查 cookie 在所有其他 REST 请求中是否相同(我已经使用 Postman 测试了 POST 方法,并且有用)。承认,这是我第一次使用 Java 编码,其中包括 REST 架构,尽管我了解基础知识的基础知识,但我选择了 google 和 SO,它们引导我来到这里。那么..我的示例中的代码是否必要或者可以简化?感谢您的回答,我非常感激:)

最佳答案

首先值得注意的是 POST /<someProjectName>/rest/user/authentication HTTP/1.1Content-Length: 0您确定要发送数据吗?

服务器也响应 415,这可能是因为您正在发送 Content-Type: text/plain; charset=UTF-8你可能应该发送 application/json; charset=UTF-8 。您可能还想将 Accept header 设置为 application/json ,告诉服务器您期望响应为 JSON。

如果您仅调用基于 JSON 的 REST API,那么直接使用 HTTP 客户端似乎级别很低。就我个人而言,我会使用 Springs RestTemplate(也可以配置为使用 HttpClient 和连接池),代码将缩短 5 倍,并且更易于阅读。

关于java - Apache HttpClient 4.x 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41567492/

相关文章:

java - 休息模板 - 没有可用的缓冲区空间(已达到最大连接数?)

java - DefaultHttpClient 在多个请求上保持 Activity 连接

java - 我的jsp中出现错误

java - java中的MQQueueManager多实例

java - 如何获取 List 或 Set 的第一个元素?

.net - 在 ASP.NET 中使用新的 session ID 打开一个新的浏览器窗口

java - Apache HTTP 客户端中的 Cookie - Java

netbeans - java中aboutBox()的问题

javascript - 第 3 方 cookie - 广告商 - 实现说明?

javascript - jQuery cookie CORS 即使设置了 header 和 withCredentials 也不起作用