0

アプリケーションの HTTP クライアントを Commons HTTP Client v3.x から新しい HTTP Client v4.x に変更しようとしています。検索しましたが、次のシナリオの適切な例が見つかりませんでした。新しい HTTP クライアントに関する優れたチュートリアル/記事 (java.net.url の SO にある優れた Community Wiki 記事に似たもの) を教えてください。

(1) クッキーポリシーの設定

(2) ホスト/ドメインとユーザー名/パスワードを定義する HTTP プロキシを設定します。

現在、これは次の方法で行われます--

Credentials credentials =
            ( host == null || domain == null || "".equals(host.trim()) ||     
           "".equals(domain.trim()) ) ?
                new UsernamePasswordCredentials(username, password) :
                new NTCredentials(username, password, host, domain);

 client.getState().setProxyCredentials( AuthScope.ANY, credentials);

(3) 認証資格情報は、次のコードを使用して古い HTTP クライアントで定義されます--

  client.getState().setCredentials(
                new AuthScope(urlObj.getHost(), urlObj.getPort()),
                new UsernamePasswordCredentials(username, password)
            );

新しい HTTP クライアントでこれを行う方法は何ですか?

(4) 新しい HTTP Method 変数を宣言し、この変数に対して method- を GET または POST として指定する

上記に現在使用されているコード--

 HttpMethodBase method;
method = createPostMethod(url, params, multipart, charset);
 method = createGetMethod(url, params, charset);

(5) メソッドへのリクエストヘッダーの追加 -

たとえば、ユーザー エージェントをデフォルトのユーザー エージェントとして設定するには、次のコードを使用します。

method.addRequestHeader(new Header("User-Agent", DEFAULT_USER_AGENT));
4

1 に答える 1

2

(1)クッキーのこと別の質問からこれを見る

(2)代理人:

httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("localhost", 8080),
                    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
HttpHost proxy = new HttpHost("localhost", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

HttpGet httpget = new HttpGet("/");

から:

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

(3)わからない

(4)方法

HttpGet httpget = new HttpGet(url);
HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
                    "org=self_registered_users&" +
                    "goto=/portal/dt&" +
                    "gotoOnFail=/portal/dt?error=true");

例の ClientFormLogin.java より

(5)ヘッダーフィールド:

HttpGet get = new HttpGet(url);
get.setHeader("Content-Type", "text/html");
get.setHeader("User-Agent","Mozilla/4.0 (MobilePhone SCP-5500/US/1.0) NetFront/3.0 MMP/2.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
get.setHeader("Accept-Charset", Chareset+";q=0.7,*;q=0.7");//"utf-8;q=0.7,*;q=0.7");
get.getParams().setParameter("http.socket.timeout",20000);

どのようにそのことについて?例を見てください。

于 2012-03-02T02:29:12.497 に答える