我一直在尝试从HTTP POST请求中获取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本身,然后我应检查所有其他REST请求中的cookie是否相同(我已经使用Postman测试了POST方法,有用)。承认,这是我第一次用Java编写,其中包括REST架构,即使我理解基础知识的基础知识,我选择google和SO引导我到这里。那么..是我的例子中的代码是必要的还是可以简化? Ty的答案,我非常感激:)答案 0 :(得分:1)
第一件事是,POST /<someProjectName>/rest/user/authentication HTTP/1.1
有Content-Length: 0
您确定要发送数据吗?
此外,服务器以415响应,这可能是因为您发送了Content-Type: text/plain; charset=UTF-8
,您应该发送application/json; charset=UTF-8
。您可能还希望将Accept标头设置为application/json
,以告诉服务器您希望响应为JSON。
如果您只调用基于JSON的REST API,则直接使用HTTP客户端似乎非常低级别。我个人会使用Springs RestTemplate(它也可以配置为使用HttpClient和连接池),代码将缩短5倍,并且更容易阅读。