-3

I have a server which I have to do login first (first URL), then I will send another POST request (second URL), but this post request can only be done if I am still logged on. How can do the POST request using URL 2 still being logged?

Here is the code:

  HttpURLConnection con=(HttpURLConnection) new URL("http://Login(URL1)").openConnection();
            String headerName=null; String cookie=null; 
            for (int i=1; (headerName = con.getHeaderFieldKey(i))!=null; i++) {
                if (headerName.equals("Set-Cookie"))               
                 cookie = con.getHeaderField(i);            
            }
            cookie = cookie.substring(0, cookie.indexOf(";"));
            String cookieName = cookie.substring(0, cookie.indexOf("="));
            String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
            System.out.println(cookieName);
            System.out.println(cookieValue);
            con=(HttpURLConnection) new URL("http://Login(URL1)").openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Cookie", "session_id=" + cookieValue);
            con.setDoInput(true);
            con.setDoOutput(true);
            PrintWriter writer = new PrintWriter(con.getOutputStream());
            String account = "sdfsdf";
            String password = "sddfdsf";
            writer.println("&username=" + account + "&password=" + password + "&action=do_login&http=1");
            writer.close();

            System.err.println(con.getResponseCode());
            BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
            String response;
            while ((response=reader.readLine())!=null) {
                System.out.println(response);
            }
            reader.close();


            con=(HttpURLConnection) new URL("http://POST Request URL").openConnection();
            con.setRequestMethod("POST")
halfer
  • 19,824
  • 17
  • 99
  • 186
user2413711
  • 85
  • 2
  • 12

2 Answers2

0

You don't need to do anything.

  1. HttpURLConnection does TCP connection pooling behind the scenes.
  2. HTTP sessions are preserved via cookies, even across multiple connections.
user207421
  • 305,947
  • 44
  • 307
  • 483
-1

Have you tried adding credentials to the request to URL 2?

UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
            username, password);
request.addHeader(new BasicScheme().authenticate(creds, request));
HttpResponse response = httpClient.execute(request);
PandaBearSoup
  • 699
  • 3
  • 9
  • 20
  • Thanks a lot for the response, The login is working in the URL 1...Even if i do it again i have to establish a different connection for the POST request...How can i do the POST securing that i am in a logged state? Btw i am using HttpURLConnection not HttpClient. – user2413711 Jun 19 '13 at 15:33
  • So the first URL is some manual login page on the server? When you login to a website or server your credentials are stored for subsequent HTTP requests sent as a result of your navigation. So, when you are sending requests through java or any other language you need to attach credentials to EACH request. – PandaBearSoup Jun 19 '13 at 15:37
  • Yes the first URL is just a UN and PW page...after login there comes service page...That is where i am not able to get the service. How can i send the credentials with out doing POST request. – user2413711 Jun 19 '13 at 15:40
  • Credentials are header fields. You can attach them to any request, be it POST or GET, the way PandaBearSoup described. – Fildor Jun 19 '13 at 16:14
  • Thank you guys for you help. But i have one Question. The cookies which i send in the second request, are not they enough for letting my request to be identified as logged on? – user2413711 Jun 19 '13 at 17:37
  • You seem to be assuming some HTP client other than what the OP is actually using. – user207421 Apr 30 '17 at 10:28