Android HttpPost一直在捕获ClientProtocolException
我不知道我做错了什么,但如果在浏览器和iPhone版本的应用程序中一切正常,我就无法让它正常工作 有什么想法吗?谢谢你的帮助 以下是错误的整个堆栈跟踪: 11-18 20:31:55.433:W/System.err1479: org.apache.http.client.ClientProtocolException 11-18 20:31:55.437: W/System.err1479:at org.apache.http.impl.client.AbstractHttpClient.executeAbstractHttpClient.java:557 11-18 20:31:55.437:W/System.err1479:at org.apache.http.impl.client.AbstractHttpClient.executeAbstractHttpClient.java:653 11-18 20:31:55.437:W/System.err1479:at org.apache.http.impl.client.AbstractHttpClient.executeAbstractHttpClient.java:627 11-18 20:31:55.437:W/System.err1479:at org.apache.http.impl.client.AbstractHttpClient.executeAbstractHttpClient.java:616 11-18 20:31:55.437:W/System.err1479:at com.example.testserverec.MainActivity$SendDataToECTask.doInBackgroundMainActivity.java:97 11-18 20:31:55.437:W/System.err1479:at com.example.testserverec.MainActivity$SendDataToECTask.doInBackgroundMainActivity.java:1 11-18 20:31:55.437:W/System.err1479:at android.os.AsyncTask$2.callAsyncTask.java:287 11-18 20:31:55.437: W/System.err1479:at java.util.concurrent.FutureTask.runFutureTask.java:234 11-18 20:31:55.437:W/System.err1479:at android.os.AsyncTask$SerialExecutor$1.runAsyncTask.java:230 11-18 20:31:55.437:W/System.err1479:at java.util.concurrent.ThreadPoolExecutor.runWorkerThreadPoolExecutor.java:1080 11-18 20:31:55.437:W/System.err1479:at java.util.concurrent.ThreadPoolExecutor$Worker.runThreadPoolExecutor.java:573 11-18 20:31:55.437:W/System.err1479:at java:85611-1820:31:55.437: W/System.err1479:原因:org.apache.http.ProtocolException: 内容长度标题已存在11-18 20:31:55.437: W/System.err1479:at org.apache.http.protocol.RequestContent.processRequestContent.java:70 11-18 20:31:55.437:W/System.err1479:at org.apache.http.protocol.BasicHttpProcessor.processBasicHttpProcessor.java:290 11-18 20:31:55.437:W/System.err1479:at org.apache.http.protocol.HttpRequestExecutor.preProcessHttpRequestExecutor.java:160 11-18 20:31:55.437:W/System.err1479:at org.apache.http.impl.client.DefaultRequestDirector.executeDefaultRequestDirector.java:408 11-18 20:31:55.437:W/System.err1479:at org.apache.http.impl.client.AbstractHttpClient.executeAbstractHttpClient.java:555 11-18 20:31:55.437:W/System.err1479:。。。还有11个Android HttpPost一直在捕获ClientProtocolException,android,http-post,Android,Http Post,我不知道我做错了什么,但如果在浏览器和iPhone版本的应用程序中一切正常,我就无法让它正常工作 有什么想法吗?谢谢你的帮助 以下是错误的整个堆栈跟踪: 11-18 20:31:55.433:W/System.err1479: org.apache.http.client.ClientProtocolException 11-18 20:31:55.437: W/System.err1479:at org.apache.http.impl.client.AbstractHttpClient.ex
因此,帮助我解决问题的是这句话: W/System.err1479:原因:org.apache.http.ProtocolException:内容长度标头已存在11-18 20:31:55.437: 只需删除:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(myURL);
// I can't show my url but it's a verified .asp working both with iOS and browser
try {
// Add your data
String str = "param1=test¶m2=test2¶m3=test3";
StringEntity strEntity = new StringEntity(str, HTTP.UTF_8);
httppost.setEntity(strEntity);
httppost.setHeader("Set-Cookie", sessionCookie);
httppost.setHeader("Accept", "text/html");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setHeader("Content-Length", strEntity.getContentLength()+"");
// Execute HTTP Post Request
BasicResponseHandler brh = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, brh);
System.out.println(responseBody);
} catch (ClientProtocolException e) {
// I keep catching this exception
e.printStackTrace();
} catch (IOException e) {
System.err.println("Check network");
}
现在我可以向服务器发送请求了。这应该可以了
httppost.setHeader("Content-Length", strEntity.getContentLength()+"");
是否提供了带有异常的消息?如果是这样,那是什么?@Charles我已经编辑了我的帖子并添加了整个堆栈跟踪,但是你会发现它并没有真正的帮助……你不想要堆栈跟踪,你想要的是异常提供的消息。它可能包含更多关于HTTP连接到底出了什么问题的信息。哦,最后我在堆栈跟踪中找到了它:由:org.apache.HTTP.ProtocolException:Content-Length头已经存在。我仍然有一个错误,但此异常不再被捕获,我的请求已正确发送到服务器。尝试了此操作,但仍收到ClientProtocolException。
HttpParams httpParams=new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpPost httppost = new HttpPost(myURL);
// I can't show my url but it's a verified .asp working both with iOS and browser
try {
// Add your data
String str = "param1=test¶m2=test2¶m3=test3";
StringEntity strEntity = new StringEntity(str, HTTP.UTF_8);
httppost.setEntity(strEntity);
httppost.setHeader("Set-Cookie", sessionCookie);
httppost.setHeader("Accept", "text/html");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setHeader("Content-Length", strEntity.getContentLength()+"");
// Execute HTTP Post Request
BasicResponseHandler brh = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, brh);
System.out.println(responseBody);
} catch (ClientProtocolException e) {
// I keep catching this exception
e.printStackTrace();
} catch (IOException e) {
System.err.println("Check network");
}