【问题标题】:HttpPost with Header, Cookies, Parameters - 417 - Expectation Failed带有标头、Cookie、参数的 HttpPost - 417 - 预期失败
【发布时间】:2011-08-07 22:09:44
【问题描述】:

好的,这将是一个长篇,所以感谢所有阅读到最后的人。

前提条件:我无法访问服务器,我只是想将数据作为新闻评论发布。

我现在拼命地测试了几个小时,但我仍然没有成功。我基本上需要的是这种请求:

POST http://www.example.com/gen/processtalkback.php
Cookie: userid=XXXX; password=XXXX
Content-Type: application/x-www-form-urlencoded
reference_id=XXXX&talkback_command=newentry&talkback_content=comment&talkback_viewpage=1&reference_area=11

所以没什么特别的,只是两个带有两个 cookie 和一个内容描述符和五个参数的标题。 这两个 cookie 是强制性的,所以我这样设置它们:

CookieStore cookieStore = httpClient.getCookieStore();

BasicClientCookie cookie = new BasicClientCookie("userid", "XXXX");
cookie.setDomain("http://www.example.com");
cookieStore.addCookie(cookie);

BasicClientCookie cookie2 = new BasicClientCookie("password", "XXXX");
cookie2.setDomain("http://www.example.com");
cookieStore.addCookie(cookie2);

之后,我将标头和内容设置为 HttpPost 对象并执行它:

HttpPost httpost = new HttpPost("http://www.example.com/gen/processtalkback.php");
httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> nvps = new ArrayList<NameValuePair>(5);
nvps.add(new BasicNameValuePair("reference_id", "XXXX"));
...
httpost.setEntity(new UrlEncodedFormEntity(nvps));

HttpResponse httpResponse = httpClient.execute(httpost);

我查看了回复,它告诉我:

Log.i("RESPONSE", httpResponseActivity.getStatusLine() + EntityUtils.toString(httpResponse .getEntity()));

HTTP/1.1 417 Expectation Failed
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
     <title>417 - Expectation Failed</title>
    </head>
    <body>
     <h1>417 - Expectation Failed</h1>
    </body>
</html>

我真的不知道问题是什么。使用诸如“poster”或“HttpRequester”之类的 Firefox 扩展,我可以成功发布:

POST http://www.example.com/gen/processtalkback.php
Cookie: userid=XXXX; password=XXXX
Content-Type: application/x-www-form-urlencoded
reference_id=XXXX&talkback_command=newentry&talkback_content=comment&talkback_viewpage=1&reference_area=11

200 OK
X-Powered-By:  PHP/5.3.6
Set-Cookie:  ...
Expires:  Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control:  no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma:  no-cache
Content-Type:  text/html; charset=utf-8
Transfer-Encoding:  chunked
Date:  Sun, 07 Aug 2011 16:24:01 GMT
Server:  lighttpd/1.4.22
 <a name="commentanchor"></a>
...

我也尝试过使用 Parameter 对象,但仍然没有成功:

HttpParams params = new BasicHttpParams();          
params.setParameter("reference_id", "XXXX");
...
httpost.setParams(params);

问题的原因可能是什么?我错过了什么吗?我不知道的任何 Apache HttpClient 细节?我知道服务器通过这次失败告诉我一些事情,所以我在网上搜索并尝试了解决方案之一:

params.setBooleanParameter("http.protocol.expect-continue", false);

还是没有成功。

我从应用程序“shark for root”的窃听中得到了什么:没有这个参数:

Data:
POST /gen/processtalkback.php
HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 119
Host: www.example.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Expect: 100-Continue

HEX: .....

现在有了参数:

Data:
POST /gen/processtalkback.php
HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 119
Host: www.example.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
reference_id=XXXX...............

HEX: .....

我通过浏览器发布的萤火虫得到了什么:

Host    www.example.de
User-Agent  Mozilla/5.0
Accept  text/javascript, text/html, application/xml, text/xml, */*
Accept-Language en-us,de-de;q=0.8,en;q=0.5,de;q=0.3
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT 1
Connection  keep-alive
X-Requested-With    XMLHttpRequest
X-Prototype-Version 1.7
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Referer ...
Content-Length  134
Cookie  ...
Pragma  no-cache
Cache-Control   no-cache

为什么 cookie 不会显示在我的程序化 httppost 尝试中?如果有人有想法,那就太棒了。 :-)

【问题讨论】:

    标签: android http-post httpcookie


    【解决方案1】:

    用另一种方法让它工作。我使用了这个德国论坛发帖的代码:http://www.android-hilfe.de/android-app-entwicklung/6398-http-post-request-mit-cookie.html

    所以我基本上不得不写

    URL url = new URL("http://www.example.com/gen/processtalkback.php");
    
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Cookie", "userid=XXXX; password=XXXX");
    connection.connect();
    

    鲨鱼转储:

    Data:
    POST /gen/processtalkback.php
    HTTP/1.1
    cookie: userid=XXXX; password=XXXX
    User-Agent: Dalvik/1.2.0 (Linux; U; Android 2.2.2; ...
    Host: www.example.com
    Connection: Keep-Alive
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 119
    
    HEX: .....
    

    帖子内容单独发送:

    Data:
    reference_id=XXXX&talkback_command=newentry&talkback_content=comment&talkback_viewpage=1&reference_area=11
    
    HEX: .....
    

    而且效果很好! 我真的很困惑,为什么这种方法在第一次就成功了,然后用 Apache HttpClient 尝试了几个小时,结果真的把我的头发扯掉了。

    有人知道更多细节吗?非常感谢任何解释。

    【讨论】:

      【解决方案2】:

      我在 Android 2.2 上遇到了同样的问题(在 Android > 2.2 上没有它们的情况下工作)。仅在使用两个参数时才对我起作用,如下所示:

      HttpParams params = new BasicHttpParams();          
      
      params.setParameter("reference_id", "XXXX");
      params.setBooleanParameter("http.protocol.expect-continue", false);
      
      httpost.setParams(params);
      

      【讨论】:

        猜你喜欢
        • 2011-07-10
        • 1970-01-01
        • 2011-12-14
        • 2012-02-25
        • 2012-06-27
        • 1970-01-01
        • 2015-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多