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

Java Apache HttpClient 4.x不支持的媒体类型,java,cookies,apache-httpclient-4.x,Java,Cookies,Apache Httpclient 4.x,我一直在尝试从HTTPPOST请求获取cookie,该请求用于使用用户名和密码在特定RESTAPI上进行身份验证。问题在于cookie存储中没有cookie(所有相关的隐藏参数都是正确的)。用于验证用户(JSON)的POST方法的主体是: { “用户名”:, “密码”: } 我正在使用以下代码: public static void main(String[] args) { String USER_AUTHENTICATION = "/user/authentication

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

{
“用户名”:,
“密码”:
}
我正在使用以下代码:

    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);
}
publicstaticvoidmain(字符串[]args){
字符串USER_AUTHENTICATION=“/USER/AUTHENTICATION”;
字符串baseUrl=”http:////rest";
HttpClient http=null;
CookieStore httpCookieStore=新的BasicCookieStore();
RequestConfig globalConfig=RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD.build();
CredentialsProvider CredentialsProvider=新的BasicCredentialsProvider();
setCredentials(AuthScope.ANY,新用户名密码凭据(“,”);
HttpClientBuilder=HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore)
.setDefaultRequestConfig(globalConfig).setDefaultCredentialsProvider(credentialsProvider);
http=builder.build();
HttpPost httpRequest=新的HttpPost(baseUrl+用户认证);
HttpResponse HttpResponse;
试一试{
httpResponse=http.execute(httpRequest);
}捕获(可丢弃错误){
抛出新的运行时异常(错误);
}
List 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! []
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缓存
12:09:34.298[main]DEBUG org.apache.http.impl.conn.poolighttpclientconnectionmanager-连接请求:[路由:{}->http://][总保持活动状态:0;分配的路由:0/2;总分配:0/20]
12:09:34.343[main]DEBUG org.apache.http.impl.conn.poolighttpclientconnectionmanager-租用的连接:[id:0][route:{}->http://][total保持活动状态:0;分配的路由:1/2;分配的总数:1/20]
12:09:34.346[main]DEBUG org.apache.http.impl.execchain.MainClientExec-打开连接{}->http://
12:09:34.355[main]DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator-连接到/
12:09:34.363[main]DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator-已建立连接:
12:09:34.363[main]DEBUG org.apache.http.impl.execchain.MainClientExec-执行请求POST//rest/user/authentication http/1.1
12:09:34.363[main]DEBUG org.apache.http.impl.execchain.MainClientExec-目标身份验证状态:未被质询
12:09:34.365[main]DEBUG org.apache.http.impl.execchain.MainClientExec-代理身份验证状态:未被质询
12:09:34.368[main]DEBUG org.apache.http.headers-http-outing-0>>POST//rest/user/authentication http/1.1
12:09:34.369[main]DEBUG org.apache.http.headers-http-outing-0>>内容长度:0
12:09:34.369[main]DEBUG org.apache.http.headers-http-outing-0>>主机:
12:09:34.369[main]DEBUG org.apache.http.headers-http-outing-0>>连接:保持活动状态
12:09:34.369[main]DEBUG org.apache.http.headers-http-outing-0>>用户代理:apache HttpClient/4.5.2(Java/1.8.091)
12:09:34.369[main]DEBUG org.apache.http.headers-http-outing-0>>接受编码:gzip,deflate
12:09:34.369[main]DEBUG org.apache.http.wire-http-outing-0>>“POST//rest/user/authentication http/1.1[\r][\n]”
12:09:34.369[main]DEBUG org.apache.http.wire-http-outing-0>>“内容长度:0[\r][\n]”
12:09:34.369[main]DEBUG org.apache.http.wire-http-outing-0>>“主机:[\r][\n]”
12:09:34.369[main]DEBUG org.apache.http.wire-http-outing-0>>“连接:保持活动[\r][\n]”
12:09:34.369[main]DEBUG org.apache.http.wire-http-outing-0>>“用户代理:apache HttpClient/4.5.2(Java/1.8.091)[\r][\n]”
12:09:34.370[main]DEBUG org.apache.http.wire-http-outing-0>>“接受编码:gzip,deflate[\r][\n]”
12:09:34.370[main]DEBUG org.apache.http.wire-http-outing-0>>“[\r][\n]”

12:09:34.406[main]DEBUG org.apache.http.wire-http-outing-0第一件突出的事情是
POST//rest/user/authentication http/1.1
具有
内容长度:0
您确定要发送数据吗

服务器还以415响应,这可能是因为您正在发送
内容类型:text/plain;charset=UTF-8
,您可能应该发送
application/json;字符集=UTF-8
。您可能还希望将Accept头设置为
application/json
,告诉服务器您希望响应是json


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

您应该更改问题的标题,它与cookies无关
httpPost.setHeader(“内容类型”、“应用程序/json”)
,但是当我看到您的示例时,我得到了
org.apache.http.headers-http-outgoing-0,看起来您是从其他地方复制的。您有CredentialsProvider,这通常用于基本身份验证,如果需要向服务发布用户/传递,则可能不应使用它。此外,您几乎从不在REST服务中使用Cookie,通常对身份验证的调用会返回一个令牌,您必须将该令牌添加到未来的请求中。对我来说,这听起来像是在你的头上。除非你能提供一个可以从互联网上访问的REST服务版本,否则我想我帮不了你。你有关于端点的文档吗?那么也许我可以构建一个虚拟应用程序
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! []