3

这真的很奇怪。我有一个看起来几乎相同但不会引发异常的函数。这是有问题的代码:

public Header[] generateHeaders() {
    Header[] headers = new Header[2];
    headers[0] = new BasicHeader("Client", "android");
    headers[1] = new BasicHeader("Accept", "application/json; charset=UTF-8");
    return headers;
}

public HttpPost getHttpRequestPost() throws URISyntaxException {
    HttpPost request = new HttpPost(new URI(generateQueryUrl()));
    request.setHeaders(generateHeaders());
    setTimeout(request);

    return request;
}

protected Query buildQuery() {
    Query query = null;

    query = new Query(new Config(), getContext());
    query.addOrderedParam(FUNCTION_NAME);

    return query;
}

下面的代码导致ClientProtocolException,正是这个:Caused by: org.apache.http.ProtocolException: Invalid header: :

public HttpResponse execute() throws URISyntaxException, ClientProtocolException,
        IOException {
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = response = client.execute(buildQuery().getHttpRequestPost());
    return response;
}

它不指向 2 个标题中的任何一个,而是指向一些空标题。这里发生了什么?

client.execute(...) 内部发生异常,显然我无法在那里调试...

这些可能是响应标头...但是我在提琴手中看不到任何问题。这是请求和响应正文:

要求

GET http://myapp.api.pl/api/bankName?accountNumber=32195000012006548541990002 HTTP/1.1
User-Agent: Fiddler
Accept: application/json
Client: android
Host: myapp.api.pl

回复

HTTP/1.1 404 Not Found
Date: Tue, 12 Nov 2013 08:35:01 GMT
Server: Apache
X-Powered-By: PleskLin
Content-Length: 0
Connection: close
Content-Type: application/json; charset=UTF-8

好吧,我撒了一点谎,在提琴手中并非一切都好。如果参数 accountNumber 指定了错误的数字,函数应该返回 404。确实如此,但在提琴手中也有错误弹出:

在此处输入图像描述

编辑:粘贴请求的堆栈跟踪:

11-08 13:09:30.691: W/System.err(20729): org.apache.http.client.ClientProtocolException
11-08 13:09:30.701: W/System.err(20729):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
11-08 13:09:30.711: W/System.err(20729):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-08 13:09:30.711: W/System.err(20729):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-08 13:09:30.721: W/System.err(20729):  at pl.test.helloapp.communication.requests.BaseRequest.execute(BaseRequest.java:75)
11-08 13:09:30.721: W/System.err(20729):  at pl.test.helloapp.async.LoginAsyncTask.doInBackground(LoginAsyncTask.java:51)
11-08 13:09:30.721: W/System.err(20729):  at pl.test.helloapp.async.LoginAsyncTask.doInBackground(LoginAsyncTask.java:1)
11-08 13:09:30.721: W/System.err(20729):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-08 13:09:30.731: W/System.err(20729):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-08 13:09:30.731: W/System.err(20729):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-08 13:09:30.731: W/System.err(20729):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-08 13:09:30.731: W/System.err(20729):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-08 13:09:30.731: W/System.err(20729):  at java.lang.Thread.run(Thread.java:841)
11-08 13:09:30.731: W/System.err(20729): Caused by: org.apache.http.ProtocolException: Invalid header: : 
11-08 13:09:30.731: W/System.err(20729):  at org.apache.http.impl.io.AbstractMessageParser.parseHeaders(AbstractMessageParser.java:162)
11-08 13:09:30.731: W/System.err(20729):  at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:178)
11-08 13:09:30.741: W/System.err(20729):  at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
11-08 13:09:30.741: W/System.err(20729):  at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
11-08 13:09:30.741: W/System.err(20729):  at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
11-08 13:09:30.741: W/System.err(20729):  at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
11-08 13:09:30.741: W/System.err(20729):  at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
11-08 13:09:30.741: W/System.err(20729):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
11-08 13:09:30.751: W/System.err(20729):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-08 13:09:30.751: W/System.err(20729):  ... 11 more
4

2 回答 2

6

服务器似乎响应无效的 http 标头。

我猜这个异常是从'org.apache.http.message.BufferedHeader'引发的:

    if (buffer == null) {
        throw new IllegalArgumentException
            ("Char array buffer may not be null");
    }
    int colon = buffer.indexOf(':');
    if (colon == -1) {
        throw new ParseException
            ("Invalid header: " + buffer.toString());
    }
    String s = buffer.substringTrimmed(0, colon);
    if (s.length() == 0) {
        throw new ParseException
            ("Invalid header: " + buffer.toString());
    }
    this.buffer = buffer;
    this.name = s;
    this.valuePos = colon + 1;

您可以通过 tcp 转储转储全部 http 正文,还是通过 curl 枚举?

于 2013-11-11T09:51:09.690 回答
0

我无法使用此代码解决问题。我用另一个交换了通信框架。Apache HTTPClient 对这些响应有问题,但是对于 HttpURLConnection 一切都很好,所以我放弃了 Apache 的使用。

于 2013-11-18T08:35:04.910 回答