这真的很奇怪.我有一个看起来几乎相同但不抛出异常的函数.这是有问题的代码:
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,正是这样:引起:org.apache.http.ProtocolException:无效的标题::
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.1User-Agent: fiddlerAccept: application/JsonClIEnt: androIDHost: myapp.API.pl
响应
http/1.1 404 Not FoundDate: Tue, 12 Nov 2013 08:35:01 GMTServer: ApacheX-Powered-By: PlesklinContent-Length: 0Connection: closeContent-Type: application/Json; charset=UTF-8
好吧我撒了一点,并不是所有东西都是小提琴手.如果参数accountNumber指定了错误的数字,则函数应返回404.确实如此,但fiddler中也有错误d出:
编辑:
粘贴请求的堆栈跟踪:
11-08 13:09:30.691: W/System.err(20729): org.apache.http.clIEnt.ClIEntProtocolException11-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.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.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
解决方法:
服务器似乎响应无效的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枚举?
总结以上是内存溢出为你收集整理的android – ProtocolException:无效的标题::在HTTPRequest上全部内容,希望文章能够帮你解决android – ProtocolException:无效的标题::在HTTPRequest上所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)