6

Java Web アプリケーションで apache http クライアント (v4) を使用していますが、次のような場合に行き詰まっており、簡単な使用例が必要です。

(1) Apache HTTP クライアントで Cookie を使用する方法、Cookie の使用に使用できるさまざまなオプション

(2) レスポンスが HTTPResponse オブジェクトで利用可能な場合、charset、mimetype、レスポンス ヘッダー (KeyValuePair として) および budy (byte[] として) を抽出します。

4

1 に答える 1

6

1) Cookie については、次の例を参照してください。

httpcomponents-client-4.1.3\examples\org\apache\http\examples\client\ClientCustomContext.java

メインコード:

HttpClient httpclient = new DefaultHttpClient();
        try {
            // Create a local instance of cookie store
            CookieStore cookieStore = new BasicCookieStore();

            // Create local HTTP context
            HttpContext localContext = new BasicHttpContext();
            // Bind custom cookie store to the local context
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            HttpGet httpget = new HttpGet("http://www.google.com/");

            System.out.println("executing request " + httpget.getURI());

            // Pass local context as a parameter
            HttpResponse response = httpclient.execute(httpget, localContext);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }

2)応答から必要なものをすべて取得できます。

HttpEntity entity = response.getEntity();
entity.getContent()

httpcomponents-client-4.1.3\examples\org\apache\http\examples\client of httpcomponents-client-4.1.3-bin.zip の例を読んでください。これは、そのWeb サイトからダウンロードされます。

于 2012-03-02T02:15:52.407 に答える