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