具有标题,Cookie,参数的HttpPost - 417 - 期望失败

时间:2011-08-07 22:09:44

标签: android http-post httpcookie

好的,这将是一个很长的,所以对于所有正在阅读这个的人来说都是如此。

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

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

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 Extensions,我成功发布了帖子:

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: .....

我从firebug通过浏览器发布的内容:

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

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

2 个答案:

答案 0 :(得分:3)

使用其他方法。我使用了德国论坛发布的代码: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尝试了几个小时导致我的头发被撕裂了。

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

答案 1 :(得分:2)

我在Android 2.2上遇到了同样的问题(没有它们适用于Android&gt; 2.2)。只有在同时使用这两个参数时才能解决这个问题:

HttpParams params = new BasicHttpParams();          

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

httpost.setParams(params);