Adding a cookie to an HTTP request

I am trying to add a cookie with a GUID to identify the user, but my code does not seem to send cookies when I run it in my test script.

My Java code is:

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&q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&offset=" + offset); //HttpPost httpPost = new HttpPost("http://vkontakte.ru/gsearch.php?section=audio&q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&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(); } 

My PHP code for checking cookies:

 <?php $cookies = $_COOKIE; if(!count($cookies) > 0) echo 'No cookies!'; foreach ($cookies as $key=>$val) echo "$key--> $val"; ?> 

The GUID is retrieved using this 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&email=" + URLEncoder.encode(username, HTTP.UTF_8) + "&pass=" + URLEncoder.encode(password, HTTP.UTF_8)); httpClient.execute(httpPost); List<Cookie> 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(); } 
+4
source share
2 answers

Setting the cookie header manually seems to work ...

  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(); HttpPost httpPost = new HttpPost("http://192.168.1.43/test/test.php"); //HttpPost httpPost = new HttpPost("http://vkontakte.ru/gsearch.php?section=audio&q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&offset=" + offset); httpPost.setHeader("Cookie", "remixsid=" + guid); HttpResponse response = httpClient.execute(httpPost); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } catch(Exception e) { System.out.println(e.toString()); } } }; searchThread.start(); } 
+5
source

A few comments and suggestions:

  • You do not add path information to the cookie. You can use the BasicClientCookie.setPath() method.
  • What is the php script path that is trying to read this cookie?
  • When a cookie is set on the root path ( / ), then it becomes available only for all other server-side programs.
  • Either set the cookie path to root, or specify the cookie path the same as the PHP script path.

EDIT:

I missed part of the domain. You also need to set a cookie domain. Assuming your PHP script is running at http://mysite/myscript.php Here mysite is the domain (it could be localhost if you use the script in your local field). Use BasicClientCookie.setDomain() to set the cookie domain.

To understand this, suppose your lib http client interacts with the website as a browser. The browser stores cookies from many sites, but when accessing a particular site, it only sends cookies that it received from this particular site.

If no domain is specified, lib will not send cookies to this host.

+2
source

Source: https://habr.com/ru/post/1415681/


All Articles