jjzjj

android - 带 header 、Cookie、参数的 HttpPost - 417 - 预期失败

coder 2023-12-28 原文

好吧,这将是一篇很长的文章,所以感谢所有读到最后的人。

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

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

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

所以没什么特别的,只有两个 header 、两个 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);

之后,我将 header 和内容设置为 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?如果有人有想法,那就太棒了。 :-)

最佳答案

用另一种方法让它工作。我使用了德国论坛发帖的代码: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 上尝试了几个小时,结果真的让我抓狂。

有没有人知道更多细节?如果有任何解释,我将不胜感激。

关于android - 带 header 、Cookie、参数的 HttpPost - 417 - 预期失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976129/

有关android - 带 header 、Cookie、参数的 HttpPost - 417 - 预期失败的更多相关文章

  1. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  2. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  3. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  4. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  5. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  6. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

  7. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  8. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  9. ruby - 字符串文字中的转义状态作为 `String#tr` 的参数 - 2

    对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一

  10. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

随机推荐