Apache HttpClient(httpclient-4.0-beta2)のhttp投稿にリファラーを追加しようとしています。
これを行うサンプルコードをいくつか見つけました。コードは機能しますが、(yikes!)内部クラスをパラメーターとして受け取るように見える(不吉な名前の)addRequestInterceptorを使用するよりも、リファラーを追加するためのより単純で簡単な方法がないかどうか疑問に思っています。
問題のコードは、以下の「//リファラーヘッダーを追加する」で始まります。私は初心者ですが、このコードは私が理解できないいくつかのことを行っています。これは本当に私のhttp投稿にリファラーを追加する最も簡単な方法ですか?
ポインタをありがとう。
// initialize request parameters
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("firstName", "John"));
formparams.add(new BasicNameValuePair("lastName", "Doe"));
// set up httppost
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost(submitUrl);
httppost.setEntity(entity);
// create httpclient
DefaultHttpClient httpclient = new DefaultHttpClient();
// add the referer header, is an inner class used here?
httpclient.addRequestInterceptor(new HttpRequestInterceptor()
{
public void process(final HttpRequest request,
final HttpContext context) throws HttpException, IOException
{
request.addHeader("Referer", referer);
}
});
// execute the request
HttpResponse response = httpclient.execute(httppost);