【问题标题】:HttpPost keeps catching ClientProtocolExceptionHttpPost 不断捕获 ClientProtocolException
【发布时间】:2013-11-18 09:08:16
【问题描述】:
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):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557) 11-18 20:31:55.437: W/System.err(1479): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653) 11-18 20:31:55.437: W/System.err(1479): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 11-18 20:31:55.437: W/System.err(1479): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) 11-18 20:31:55.437: W/System.err(1479): 在 com.example.testserverec.MainActivity$SendDataToECTask.doInBackground(MainActivity.java:97) 11-18 20:31:55.437: W/System.err(1479): 在 com.example.testserverec.MainActivity$SendDataToECTask.doInBackground(MainActivity.java:1) 11-18 20:31:55.437: W/System.err(1479): 在 android.os.AsyncTask$2.call(AsyncTask.java:287) 11-18 20:31:55.437: W / System.err(1479):在 java.util.concurrent.FutureTask.run(FutureTask.java:234) 11-18 20:31:55.437:W/System.err(1479):在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 11-18 20:31:55.437:W/System.err(1479):在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 11-18 20:31:55.437: W/System.err(1479): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 11-18 20:31:55.437: W/System.err(1479): 在 java.lang.Thread.run(Thread.java:856) 11-18 20:31:55.437: W/System.err(1479):引起:org.apache.http.ProtocolException: Content-Length 标头已经存在 11-18 20:31:55.437: W / System.err(1479):在 org.apache.http.protocol.RequestContent.process(RequestContent.java:70) 11-18 20:31:55.437: W/System.err(1479): 在 org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290) 11-18 20:31:55.437: W/System.err(1479): 在 org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160) 11-18 20:31:55.437: W/System.err(1479): 在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:408) 11-18 20:31:55.437: W/System.err(1479): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 11-18 20:31:55.437: W/System.err(1479): ... 11 更多

【问题讨论】:

  • 是否提供了异常消息?如果有,是什么?
  • @Charles 我已经编辑了我的帖子并添加了整个堆栈跟踪,但你会发现它并没有真正的帮助......
  • 你不想要堆栈跟踪,你想要异常提供的消息。它可能包含有关 HTTP 连接具体出了什么问题的更多信息。
  • 哦,我终于在堆栈跟踪中找到了它:Caused by: org.apache.http.ProtocolException: Content-Length header already present。我仍然有错误,但不再捕获此异常,我的请求已正确发送到服务器。

标签: android http-post


【解决方案1】:

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

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

只需要删除:

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

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

【讨论】:

    【解决方案2】:

    这应该可以工作

    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");
    }
    

    【讨论】:

    • 试过了,但还是遇到了 ClientProtocolException。
    猜你喜欢
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多