带有标头、Cookie、参数的 HttpPost - 417 - 预期失败
好吧,这将是一篇很长的文章,所以感谢所有读到最后的人。
前提条件:我无法访问服务器,我只是想将数据作为新闻评论发布。
我现在正在拼命测试几个小时,但仍然没有成功。我基本上需要的是这种类型的请求:
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>
...
我也尝试使用参数对象,但仍然没有成功:
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?如果有人有想法那就太棒了。 :-)
Ok this will be a long one, so thx for all who are reading this to the end.
Precondition: I can't access the server, I'm just trying to post data as a news comment.
I'm desperately testing this out for a couple of hours now but I still don't have any success. What I basically need is this kind of request:
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
So nothing special, just two headers with two cookies and a content discriptor and five parameters.
The two cookies are mandatory, so I set them like this:
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);
After that I set the header and content to the HttpPost object and execute it:
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);
I look into the response and it shows me:
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>
I really don't know what the problem is. With Firefox Extensions like "poster" or "HttpRequester" I succeed with the posting:
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>
...
I also tried it with a Parameter object but still no success:
HttpParams params = new BasicHttpParams();
params.setParameter("reference_id", "XXXX");
...
httpost.setParams(params);
What could be the cause of the problem? Am I missing something? Any Apache HttpClient specifics I'm unaware of? I'm aware of the fact that the server is telling me something by this failure, so I searched in the web and tried one of the solutions for this:
params.setBooleanParameter("http.protocol.expect-continue", false);
Still no success.
What I got from wiretapping by the app "shark for root" without this parameter:
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: .....
And now with the parameter:
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: .....
What I got by firebug from posting by browser:
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
Why won't the cookies be displayed in my programmatic httppost attempts? It would be awesome if someone would have an idea. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用另一种方法让它工作。我使用了德国论坛帖子的代码: http://www.android-hilfe.de/android-app-entwicklung/6398-http-post-request-mit-cookie.html
所以我基本上不得不写
Shark dump:
帖子内容是发送单独:
而且效果非常好!
我真的很困惑为什么这种方法第一次就成功了,并且用 Apache HttpClient 尝试了几个小时,结果真的让我抓狂。
有谁知道更多细节吗?我非常感谢任何解释。
Got it working with another approach. I used this code of a German forum posting: http://www.android-hilfe.de/android-app-entwicklung/6398-http-post-request-mit-cookie.html
So I basically had to write
Shark dump:
Post content was send seperately:
And it worked perfectly!
I'm really confused why this approach was successful at the very first time and trying it with the Apache HttpClient for hours resulted in literally tearing my hair out.
Does anyone know more details? I would very much appreciate any explanation.
我在 Android 2.2 上遇到了同样的问题(对于 Android > 2.2,没有它们也可以工作)。仅当使用这两个参数时才对我起作用,如下所示:
I had the same problem on Android 2.2 (worked without them for Android > 2.2). Worked fom me only when using both params, like this: