gpt4 book ai didi

Android: org.apache.http.NoHttpResponseException: 目标服务器响应失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:06 25 4
gpt4 key购买 nike

我的 Android 应用程序使用 HttpClient/HttpGet 来访问 REST API。我还设置了:

httpGet.addHeader("Authorization", "Basic " + basicAuth);

... 将 Base64 编码的“用户名:密码”发送到服务器。为了测试,我只使用 HTTP 而不是 HTTPS 连接到服务器。

每当我在 HttpClient 上调用“执行”时,我都会收到以下异常:

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)

注意:

我实际上有三台开发服务器,它们分别来自不同的托管服务提供商。当连接到其中两个时,出现上述错误。连接到第三台服务器时,一切正常。此外,其他 GET 和 POST 请求在所有三个开发服务器上都可以正常工作。

所以我想知道在设置基本身份验证 header 时是否有关于 HTTP GET 的特定内容。

我也可以使用“curl”调用有问题的 REST API,没有任何问题。

代码:

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();
}
}

最佳答案

我终于明白问题出在哪里了。当我对用户名和密码进行 Base64 编码时,我对 Base64.encodeToString(...) 使用了错误的标志。所以我改变了错误:

来自:

basicAuth = Base64.encodeToString(basicAuth.getBytes(), Base64.DEFAULT);

收件人:

basicAuth = Base64.encodeToString(basicAuth.getBytes(), Base64.NO_WRAP);

...一切正常。

关于Android: org.apache.http.NoHttpResponseException: 目标服务器响应失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11703689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com