Announcement Announcement Module
Collapse
No announcement yet.
Sending cookies as headers with HttpHeaders Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sending cookies as headers with HttpHeaders

    Hello. I have problem with sending cookies with my GET method. Here's what I do:

    1. I receive a proper cookie from server by making POST with username & password (server log shows that the request was valid and sends me a cookie with the response headers)
    2. I parse the cookie header to receive only the logged_in part.
    3. I set The cookie as a header:
    Code:
                    RestTemplate restTemplate = new RestTemplate();
    		restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());	
    		
    		HttpHeaders requestHeaders = new HttpHeaders();
    		requestHeaders.set("Cookie", this.sessionCookie);
    			
    		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    		return restTemplate.getForObject(baseUrl+query, JsonElement.class, requestEntity);
    I also make sure that my request headers contain the cookie with it's value by logging all the headers before sending the request.
    In the server log i can see my request but it doesn't contain the cookie header? What may be the reason of this situation?

    //EDIT: I made an additional experiment and used Java's DefaultHttpClient with producing the same GET request:
    Code:
    	DefaultHttpClient httpclient = new DefaultHttpClient();
    
    	    HttpGet httpget = new HttpGet(myUrl);
    	    httpget.addHeader("Cookie", this.sessionCookie);
    	    try {
    			HttpResponse response = httpclient.execute(httpget);
    		} catch (ClientProtocolException e) {
    
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    With this code Server registered the request with Cookie header. It shows, that there is something wrong by the Spring (not my server) side. Why doesn't requestEntity send the header with cookie that I'm trying to add? Please help!
    Last edited by blackMasoon; Apr 19th, 2013, 08:06 AM.
Working...
X