2017-07-05 9 views
0

Apache HttpComponents/HttpClient Fluent APIの使用方法を理解し、ログインが必要なWebサーバーにCookieを正しく返送してから、Cookieを返送するのに問題があります。サイトの他の部分にアクセスする。私はバージョン4.5.3を使用しています。Java Apache HttpClient Fluent APIを使用したCookie管理

Fluent APIチュートリアルによれば、(HttpComponents)Executorを使用して、特定のセキュリティコンテキストでリクエストを実行し、認証の詳細をキャッシュし、後続のリクエストで再利用することができます。 https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fluent.html

は、だから私はそれを試してみましたが、私は、ログイン後に別のページにアクセスしようとする上で拒否された403回のアクセスを得る ここで私が試したコードがあります:。

CookieStore httpCookieStore = new BasicCookieStore(); 
List<Cookie> cookies = httpCookieStore.getCookies(); 

String username = "admin"; 
String password = "admin"; 
Executor httpExecutor = Executor.newInstance().auth(username, password); 
httpExecutor.use(httpCookieStore); 
Response response = httpExecutor.execute(Request.Get("http://myserver.example.com/login").useExpectContinue()); 
String loginOutput = response.returnContent().asString(); 
System.out.println(loginOutput); // works - gets the expected page 

String swaggerUrl = "http://myserver.example.com/swagger/index.html"; 
response = httpExecutor.execute(Request.Get(swaggerUrl)); 
HttpResponse httpResponse = response.returnResponse(); 
System.out.println(httpResponse); // Response is 403 Access Denied 
// prints: HttpResponseProxy{HTTP/1.1 403 Access Denied [X-Content-Type-Options: nosniff, X-XSS-Protection: 1; mode=block, Pragma: no-cache, X-Frame-Options: DENY, Content-Type: text/html;charset=ISO-8859-1, Cache-Control: must-revalidate,no-cache,no-store, Content-Length: 1391, Server: Jetty(8.1.16.v20140903)] [Content-Type: text/html; charset=ISO-8859-1,Content-Length: 1391,Chunked: false]} 

私はCookieStoreの内容を確認しましたログイン後、それが正しいクッキーを持っています

System.out.println(httpCookieStore.getCookies().size()); // prints 1 
System.out.println(httpCookieStore.getCookies().get(0)); 
// prints: [version: 0][name: JSESSIONID][value: 14b1yp7hf85es1vc6zpe2y376n][domain: myserver.example.com][path: /][expiry: null] 

だから私はまた、GETリクエストのヘッダにクッキーを追加し、明示的に試してみましたが、それはまだ同じ403アクセス拒否エラーで失敗します:

CookieStore httpCookieStore = new BasicCookieStore(); 
List<Cookie> cookies = httpCookieStore.getCookies(); 

String username = "admin"; 
String password = "admin"; 
Executor httpExecutor = Executor.newInstance().auth(username, password); 
httpExecutor.use(httpCookieStore); 
Response response = httpExecutor.execute(Request.Get("http://myserver.example.com/login").useExpectContinue()); 
String loginOutput = response.returnContent().asString(); 
System.out.println(loginOutput); // works - gets the expected page 

String swaggerUrl = "http://myserver.example.com/swagger/index.html"; 
response = httpExecutor.execute(Request.Get(swaggerUrl).addHeader(authCookieName, authCookieValue)); 
HttpResponse httpResponse = response.returnResponse(); 
System.out.println(httpResponse); // Response is 403 Access Denied 

流暢APIで、この作品を作るためにどのように任意のアイデア?

答えて

1

のHttpClientのドキュメントを見てみると、あなたがリンクされ、彼らはこの警告(強調鉱山)で開始:

As of version of 4.2 HttpClient comes with an easy to use facade API based on the concept of a fluent interface. Fluent facade API exposes only the most fundamental functions of HttpClient and is intended for simple use cases that do not require the full flexibility of HttpClient. For instance, fluent facade API relieves the users from having to deal with connection management and resource deallocation.

あなたがうまく流暢APIを介してサポートされていない構成の場合にはあるかもしれない(とあなたに答えるためにドキュメント、認証、セッション管理の引用はリンクされていますが、互いに必須ではありません。セッションを開始することなくすべてのリクエストで認証情報を要求するステートレスサービスがあります。 Cookie))

あなたは頼りにする必要があるかもしれません他のより冗長なAPI:

BasicCookieStore cookieStore = new BasicCookieStore(); 
CloseableHttpClient httpclient = HttpClients.custom() 
    .setDefaultCookieStore(cookieStore) 
    .build(); 
try { 
    HttpGet httpget = new HttpGet("https://someportal/"); 
    CloseableHttpResponse response1 = httpclient.execute(httpget); 
    try { 
     ... 
+0

礼儀正しくない「冗長」APIに切り替えることができます。 – quux00

+0

私はいつもこれを述べるAPIを避けようとしています(提供されている機能が十分であっても、すべての設定オプションを使用したい場合は、他のAPIを利用できるように、機能上の設定の簡素化を行います)。 /私は、このAPIがどのようにシーンの背後にある他のものを構成しているかを理解しようと時間を費やしたくない、2 /私は以前に働いていたことを回帰テストしたくない(特にネットワークに関係する場合) 、3 /私はそれらのAPIはバージョンをアップグレードするときに頻繁にブレーキをかけがちであることがわかります – Thierry

関連する問題