title: Adding a cookie to a HTTP request
body: <p>I'm trying to add a cookie with a GUID to identify the user but my code doesn't seem to send any cookies when I run it on my test script.</p>
  
  <p>My Java code:</p>
  
  <pre><code>    public void search(String q, int o) {
      final String query = q;
      final int offset = o;
      Thread searchThread = new Thread() {
          public void run() {
              try {
                  DefaultHttpClient httpClient = new DefaultHttpClient();
                  BasicHttpContext httpContext = new BasicHttpContext();
                  CookieStore cookieStore = new BasicCookieStore();
                  httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
                  cookieStore.addCookie(new BasicClientCookie("remixsid", guid));
                  httpClient.setCookieStore(cookieStore);
                  HttpPost httpPost = new HttpPost("http://192.168.1.43/test/test.php?section=audio&amp;q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&amp;offset=" + offset);
                  //HttpPost httpPost = new HttpPost("http://vkontakte.ru/gsearch.php?section=audio&amp;q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&amp;offset=" + offset);
                  HttpResponse response = httpClient.execute(httpPost, httpContext);
                  String responseBody = EntityUtils.toString(response.getEntity());
                  System.out.print(responseBody);
              } catch(Exception e) {
                  System.out.println(e.toString());
              }
          }
      };
      searchThread.run();
  }
  </code></pre>
  
  <p>My PHP code to test for cookies:</p>
  
  <pre><code>&lt;?php
  $cookies = $_COOKIE;
  
  if(!count($cookies) &gt; 0)
      echo 'No cookies!';
  
  foreach ($cookies as $key=&gt;$val)
      echo "$key--&gt; $val";
  
  ?&gt;
  </code></pre>
  
  <p>The GUID is retrieved with this code:</p>
  
  <pre><code>    public void login(String usr, String pass) {
      logout();
      final String username = usr;
      final String password = pass;
      Thread loginThread = new Thread() {
          public void run() {
              try {
                  DefaultHttpClient httpClient = new DefaultHttpClient();
                  HttpPost httpPost = new HttpPost("http://login.vk.com/?act=login&amp;email=" + URLEncoder.encode(username, HTTP.UTF_8) + "&amp;pass=" + URLEncoder.encode(password, HTTP.UTF_8));
                  httpClient.execute(httpPost);
                  List&lt;Cookie&gt; cookies = httpClient.getCookieStore().getCookies();
                  for(Cookie cookie : cookies) {
                      System.out.println("Cookie: " + cookie.toString());
                      if(cookie.getName().contains("remixsid"))
                          guid = cookie.getValue();
                  }
              } catch(Exception e) {
                  System.out.println(e.toString());
                  System.out.println("An error occured");
                  if(loginHandler != null)
                      loginHandler.onLoginError(e);
                  return;
              }
              if(!isLoggedIn()) {
                  System.out.println("User credentials invalid");
                  if(loginHandler != null)
                      loginHandler.onLoginInvalidCredentials();
                  return;
              }
              if(loginHandler != null)
                  loginHandler.onLoginSuccess();
              System.out.println("Login successfull GUID is: " + guid);
          }
      };
      loginThread.start();
  }
  </code></pre>