HttpPost不断捕获ClientProtocolException

时间:2013-11-18 09:08:16

标签: android http-post

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&param2=test2&param3=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");
}

我不知道我做错了什么,但是当它在浏览器和我的应用程序的iPhone版本中都可以正常工作时我无法正常工作。

所以任何想法?谢谢你的帮助。

以下是错误的整个堆栈跟踪:

  

11-18 20:31:55.433:W / System.err(1479):   org.apache.http.client.ClientProtocolException 11-18 20:31:55.437:   W / System.err(1479):at   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)   11-18 20:31:55.437:W / System.err(1479):at   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)   11-18 20:31:55.437:W / System.err(1479):at   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)   11-18 20:31:55.437:W / System.err(1479):at   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)   11-18 20:31:55.437:W / System.err(1479):at   com.example.testserverec.MainActivity $ SendDataToECTask.doInBackground(MainActivity.java:97)   11-18 20:31:55.437:W / System.err(1479):at   com.example.testserverec.MainActivity $ SendDataToECTask.doInBackground(MainActivity.java:1)   11-18 20:31:55.437:W / System.err(1479):at   android.os.AsyncTask $ 2.call(AsyncTask.java:287)11-18 20:31:55.437:   W / System.err(1479):at   java.util.concurrent.FutureTask.run(FutureTask.java:234)11-18   20:31:55.437:W / System.err(1479):at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)11-18   20:31:55.437:W / System.err(1479):at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)   11-18 20:31:55.437:W / System.err(1479):at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573)   11-18 20:31:55.437:W / System.err(1479):at   java.lang.Thread.run(Thread.java:856)11-18 20:31:55.437:   W / System.err(1479):引起:org.apache.http.ProtocolException:   内容长度标题已存在11-18 20:31:55.437:   W / System.err(1479):at   org.apache.http.protocol.RequestContent.process(RequestContent.java:70)   11-18 20:31:55.437:W / System.err(1479):at   org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290)   11-18 20:31:55.437:W / System.err(1479):at   org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160)   11-18 20:31:55.437:W / System.err(1479):at   org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:408)   11-18 20:31:55.437:W / System.err(1479):at   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)   11-18 20:31:55.437:W / System.err(1479):... 11 more

2 个答案:

答案 0 :(得分:0)

所以帮助我解决问题的是这一行:

  

W / System.err(1479):引起:org.apache.http.ProtocolException:Content-Length标头已存在11-18 20:31:55.437:

只需删除:

httppost.setHeader("Content-Length", strEntity.getContentLength()+"");

现在我可以将我的请求发送到服务器了。

答案 1 :(得分:0)

这应该有效

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&param2=test2&param3=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");
}