0 Replies Latest reply: Aug 18, 2008 9:43 PM by 807589 RSS

    Unable to get cookies

    807589
      Hi ALl

      am trying to read cookies from a URL.
      I am always getting cookies array count as 0.
      Pls see code below:



      import java.io.FileOutputStream;
      import java.io.IOException;

      import org.apache.commons.httpclient.*;
      import org.apache.commons.httpclient.HttpMethod;
      import org.apache.commons.httpclient.methods.GetMethod;
      import org.apache.commons.httpclient.HttpClient.*;


      public class TestCookies {

           /**
           * @param args
           */
           

           private static String url = "http://www.google.com";
           

           public static void main(String[] args) {


           HttpClient client = new HttpClient();
           HttpMethod method = new GetMethod(url);
           try{
           int statusCode = client.executeMethod(method);
           System.out.println(method.getResponseBodyAsString());
           byte [] res = method.getResponseBody();
           
           Cookie[] cookie= client.getState().getCookies();
           System.out.println(cookie.length);
           
           FileOutputStream fos= new FileOutputStream("C:\\donepage.html");
           fos.write(res);

           //release connection
           method.releaseConnection();
           }
           catch(IOException e) {
           e.printStackTrace();
           }
           }
           }


      If I change the above url to my company url,I will see cookies length as 0.

      I spent lot of time but couldnt figure out.

      If I open the google in fiefox i see 7 cookies but with my code am getting only 1 cookie named - PREF.
      For my company url nothing is coming

      ANy help would be appreciated.

      Thanks

      Edited by: sriyak on Aug 18, 2008 7:42 PM