2011-08-07 4 views
3

좋습니다. 이렇게하면 길어 지므로 끝까지 읽는 모든 사람에게 도움이됩니다.헤더, 쿠키, 매개 변수가있는 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 

그래서 아무것도 두 쿠키 및 콘텐츠 discriptor와 5 개 개의 매개 변수와 두 헤더 : 내가 기본적으로 필요한 것은 요청의 종류이다. 두 쿠키는 필수입니다, 그래서 나는이처럼 설정 : 나는 HttpPost 객체에 헤더와 내용을 설정 그 후

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

나는 정말로이 문제가 무엇인지 모른다. "포스터"또는 "HttpRequester"와 같은 파이어 폭스 확장 기능으로 내가 게시와 성공 :

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

의 원인이 될 수있는 무엇 :

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

나는 또한 매개 변수 개체와 여전히 성공없이 그것을 시도 문제? 내가 놓친 게 있니? 아파치 HttpClient의 특징은 무엇입니까? 나는 서버가 저에게이 실패로 뭔가를 말하고 있다는 사실을 알고 있어요, 그래서 웹에서 검색이에 대한 솔루션 중 하나 시도하지 : 여전히 성공을

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

합니다.

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

내가 브라우저에 의해 게시에서 방화범에 의해 가지고 무엇 : 매개 변수와 함께 지금

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

그리고 :이 매개 변수없이 응용 프로그램에 의해 도청에서 "루트 상어"를 가지고 무엇

:

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 시도에서 쿠키가 표시되지 않는 이유는 무엇입니까? 누군가가 아이디어를 갖고 있다면 그것은 대단 할 것입니다. :-)

답변

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: 
reference_id=XXXX&talkback_command=newentry&talkback_content=comment&talkback_viewpage=1&reference_area=11 

HEX: ..... 
:

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

포스트의 내용은 별도로 보내였다

그리고 완벽하게 작동했습니다! 나는이 접근법이 처음 성공적이었던 이유를 정말로 혼란스럽게 생각하고, 아파치 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);