这真的很奇怪。我有一个看起来几乎相同但不抛出异常的函数。这是有问题的代码:
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(...)中发生异常,显然我无法在那里调试......
那些可能是响应标题...但是我在fiddler中看不到任何错误。这是请求和响应机构:
请求
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.确实如此,但fiddler中也有错误弹出:
编辑: 粘贴请求的堆栈跟踪:
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
答案 0 :(得分: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 dump转储整个http体,还是通过curl枚举?
答案 1 :(得分:0)
我无法使用此代码解决问题。我与另一个交换框架。 Apache HTTPClient对这些响应有问题,但对于HttpURLConnection,一切都很好,所以我放弃了Apache的使用。