title: HttpPost with Header, Cookies, Parameters - 417 - Expectation Failed
body: <p>Ok this will be a long one, so thx for all who are reading this to the end.</p>
  
  <p>Precondition: I can't access the server, I'm just trying to post data as a news comment.</p>
  
  <p>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:</p>
  
  <pre><code>POST http://www.example.com/gen/processtalkback.php
  Cookie: userid=XXXX; password=XXXX
  Content-Type: application/x-www-form-urlencoded
  reference_id=XXXX&amp;talkback_command=newentry&amp;talkback_content=comment&amp;talkback_viewpage=1&amp;reference_area=11
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>After that I set the header and content to the HttpPost object and execute it:</p>
  
  <pre><code>HttpPost httpost = new HttpPost("http://www.example.com/gen/processtalkback.php");
  httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");
  List&lt;NameValuePair&gt; nvps = new ArrayList&lt;NameValuePair&gt;(5);
  nvps.add(new BasicNameValuePair("reference_id", "XXXX"));
  ...
  httpost.setEntity(new UrlEncodedFormEntity(nvps));
  
  HttpResponse httpResponse = httpClient.execute(httpost);
  </code></pre>
  
  <p>I look into the response and it shows me:</p>
  
  <pre><code>Log.i("RESPONSE", httpResponseActivity.getStatusLine() + EntityUtils.toString(httpResponse .getEntity()));
  
  HTTP/1.1 417 Expectation Failed
  &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
      &lt;head&gt;
       &lt;title&gt;417 - Expectation Failed&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
       &lt;h1&gt;417 - Expectation Failed&lt;/h1&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I really don't know what the problem is. With Firefox Extensions like "poster" or "HttpRequester" I succeed with the posting:</p>
  
  <pre><code>POST http://www.example.com/gen/processtalkback.php
  Cookie: userid=XXXX; password=XXXX
  Content-Type: application/x-www-form-urlencoded
  reference_id=XXXX&amp;talkback_command=newentry&amp;talkback_content=comment&amp;talkback_viewpage=1&amp;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
   &lt;a name="commentanchor"&gt;&lt;/a&gt;
  ...
  </code></pre>
  
  <p>I also tried it with a Parameter object but still no success:</p>
  
  <pre><code>HttpParams params = new BasicHttpParams();          
  params.setParameter("reference_id", "XXXX");
  ...
  httpost.setParams(params);
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>params.setBooleanParameter("http.protocol.expect-continue", false);
  </code></pre>
  
  <p>Still no success.</p>
  
  <p>What I got from wiretapping by the app "shark for root" without this parameter:</p>
  
  <pre><code>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: .....
  </code></pre>
  
  <p>And now with the parameter:</p>
  
  <pre><code>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: .....
  </code></pre>
  
  <p>What I got by firebug from posting by browser:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Why won't the cookies be displayed in my programmatic httppost attempts? It would be awesome if someone would have an idea. :-)</p>