HTTP POST要求からCookieを取得しようとしましたが、これはユーザー名とパスワードで特定のREST APIで認証するために使用されます。問題は、クッキーストア内にクッキーが存在しないことです(すべての関連する隠しパラメータが正しい)。ユーザ(JSON)を認証するために使用されるPOSTメソッドの本体である:コンソール出力がApache HttpClient 4.xでサポートされていないメディアタイプ
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! []
{
"username": <username>,
"password": <password>
}
Iは、次のコードを使用してい
私の考え:サポートされていないメディアタイプに問題があるかもしれませんが、これはコンソールの出力で見ることができますが、解決方法はわかりません:(
編集:私は、アプリケーションに関する追加情報を提供するためのアドバイスを受けていますので、ここに行きます:タスクは、REST APIにさまざまなリクエストで渡されるCookieを取得することです。これを行うには、私は最初に、ユーザー名とパスワードを使ってCookie自体を提供する認証をしなければなりません。そして、私はCookieが他のすべてのREST要求と同じであるかどうかをチェックしなければなりません。できます)。これはJavaのコーディングであり、基本的な基礎を理解していても、RESTアーキテクチャーを含んでいます。私はgoogleを選択しました。だから、私の例のコードは必要ですか、それとも簡略化できますか?回答のためのTy、私は非常に感謝しています:)
タイトルは質問に変更する必要があります。クッキーとは関係ありません。 –