私のAndroidアプリは、HttpClient/HttpGetを使用してRESTAPIにアクセスします。私も設定します:
httpGet.addHeader("Authorization", "Basic " + basicAuth);
... Base64でエンコードされた「username:password」をサーバーに送信します。テストでは、サーバーへの接続にHTTPSではなくHTTPを使用します。
HttpClientで「execute」を呼び出すと、次の例外が発生します。
07-28 10:51:39.610: I/# PA #(12112): org.apache.http.NoHttpResponseException: The target server failed to respond
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-28 10:51:39.610: I/# PA #(12112): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-28 10:51:39.610: I/# PA #(12112): at org.thomasamsler.android.app.tasks.HttpGetTask.doInBackground(HttpGetTask.java:87)
07-28 10:51:39.610: I/# PA #(12112): at org.foo.android.app.tasks.HttpGetTask.doInBackground(HttpGetTask.java:1)
07-28 10:51:39.610: I/# PA #(12112): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-28 10:51:39.610: I/# PA #(12112): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-28 10:51:39.610: I/# PA #(12112): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-28 10:51:39.610: I/# PA #(12112): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-28 10:51:39.610: I/# PA #(12112): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-28 10:51:39.610: I/# PA #(12112): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-28 10:51:39.610: I/# PA #(12112): at java.lang.Thread.run(Thread.java:856)
ノート:
私は実際に異なるホスティングプロバイダーを持つ3つの開発サーバーを持っています。2つに接続すると、上記のエラーが発生します。3番目のサーバーに接続すると、すべてが正常に機能します。また、他のGETおよびPOSTリクエストは、3つの開発サーバーすべてで問題なく機能します。
したがって、基本認証ヘッダーを設定するときにHTTPGETに固有の何かがあるかどうか疑問に思っています。
「curl」を使用して、問題のRESTAPIを問題なく呼び出すこともできます。
コード:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public class HttpGetTask extends AsyncTask<HttpGetRequestArgs, Void, String> implements AppConstants {
private HttpTaskNotifier mNotifier;
private HttpResponse mHttpResponse;
private int mHttpStatusCodeOk;
private int mHttpStatusCode;
private int mErrorCode = AppConstants.NO_ERROR;
public HttpGetTask(int httpStatusCodeOk, HttpTaskNotifier notifier) {
mNotifier = notifier;
mHttpStatusCodeOk = httpStatusCodeOk;
}
@Override
protected String doInBackground(HttpGetRequestArgs... args) {
HttpGetRequestArgs httpGetRequestArgs = args[0];
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = null;
try {
httpGet = new HttpGet(httpGetRequestArgs.getUrl());
String apiKey = httpGetRequestArgs.getApiKey();
if (null != apiKey && !"".equals(apiKey)) {
httpGet.addHeader("Authorization", "ApiKey " + apiKey);
}
String basicAuth = httpGetRequestArgs.getBasicAuth();
if (null != basicAuth && !"".equals(basicAuth)) {
httpGet.addHeader("Authorization", "Basic " + basicAuth);
}
}
catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "EXCEPTION", e);
return null;
}
try {
mHttpResponse = httpClient.execute(httpGet);
}
catch (ClientProtocolException e) {
mErrorCode = AppConstants.REST_ERROR;
return null;
}
catch (IOException e) {
mErrorCode = AppConstants.REST_ERROR;
return null;
}
catch (IllegalArgumentException iae) {
mErrorCode = AppConstants.REST_ERROR;
return null;
}
if (null == mHttpResponse) {
mErrorCode = AppConstants.REST_ERROR;
return null;
}
mHttpStatusCode = mHttpResponse.getStatusLine().getStatusCode();
if (mHttpStatusCodeOk != mHttpStatusCode) {
mErrorCode = AppConstants.REST_ERROR;
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpEntity httpEntity = mHttpResponse.getEntity();
if (null == httpEntity) {
mErrorCode = AppConstants.REST_ERROR;
return null;
}
try {
httpEntity.writeTo(out);
out.close();
}
catch (IOException ioe) {
mErrorCode = AppConstants.REST_ERROR;
return null;
}
return out.toString();
}
@Override
protected void onPostExecute(String content) {
if (AppConstants.NO_ERROR != mErrorCode) {
mNotifier.onError(mErrorCode);
}
mNotifier.doProcess(content);
}
@Override
protected void onCancelled() {
mNotifier.doCancel();
}
}