これは長いものになるので、これを最後まで読んでいるすべての人にthx.
前提条件: サーバーにアクセスできません。データをニュース コメントとして投稿しようとしています。
これを数時間必死にテストしていますが、まだ成功していません。私が基本的に必要とするのは、この種の要求です:
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
特別なことは何もありません。2 つの Cookie を持つ 2 つのヘッダーと、コンテンツ ディスクリプターと 5 つのパラメーターだけです。2 つの 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の詳細はありますか? サーバーがこの失敗によって何かを伝えているという事実を認識しているので、Web で検索し、これに対する解決策の 1 つを試しました。
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 が表示されないのはなぜですか? 誰かがアイデアを持っていれば素晴らしいでしょう。:-)