3

I'm dealing with a HTTP sockets project, I have to send 2 HTTP GET requests from my java code like this :

  • Request 1 : call page X
  • Page X is setting a cookie.
  • Request 2 : call to page Y

as you see to access the content of page Y i cookie must be present...

how to accept a cookie from java code please ?

Here's a sample sent request :

      String sServer = "example.com";
          InetAddress inaddr = null;

          try {
              inaddr = InetAddress.getByName(sServer);
      }
      catch (UnknownHostException ex) { //The host could not be resolved.
              System.out.println(ex);
              System.out.println("Error resolving hostname for '" + sServer + "'.\n");

      }
      Socket sock = null;
      try {
              sock = new Socket(inaddr, 80);
      }
      catch (IOException ex) {
          System.out.println(ex);
          System.out.println("Could not create the socket.\n");

      }

try {
java.io.OutputStream os = sock.getOutputStream();
 String sPacket = "GET /xxx/xxx/xxx.do HTTP/1.1\n"                                                              + "Host: example.com\n"
+ "Connection: keep-alive\n"                                                    + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.1.249.1064 Safari/532.5\n"
+ "Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8\n\n";

              os.write(sPacket.getBytes(), 0, sPacket.length());

              //Let's get the answer.
              System.out.print("The server (" + sServer + ") answered: '");
              java.io.InputStream is = sock.getInputStream();
              byte[] buf = new byte[1024];
              is.read(buf, 0, buf.length);
              for (int i = 0; i < buf.length; i++) {
                      if (buf[i] == 0) break;
                      else
                          System.out.print(new Character((char)buf[i]));
              }
              System. out.print("'\n");

      }
      catch (Exception ex) {
          System.out.println(ex);

      }    
6
  • 1
    at least post the code you've written so far, rather than asking us to do it all for you Commented May 8, 2010 at 21:59
  • 1
    Hello, thank you for replying, i'm not asking you to do it all no, i just need the name of the class/method responsible for this :) i updated my post sorry. Commented May 8, 2010 at 22:02
  • Why are you re-implementing HTTP, when Java has a perfectly good HTTP API? Commented May 8, 2010 at 22:10
  • AAGHH! My eyes! If you use an IDE like Eclipse or NetBeans, please use code formatting. In eclipse it is: Ctrl - Shift - F. Commented May 8, 2010 at 22:10
  • Sorry guys for the ugly code :) I'll try to rewrite this using java's httpclient. thanks.. Commented May 8, 2010 at 22:24

1 Answer 1

1

I would strongly recommend using HttpClient or another library dedicated to HTTP processing, rather than trying to implement/interpret the protocol yourself. See here for how to handle cookies using HttpClient.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.