1

i need browse the website by java application.

After the login i need invoke form.

My code is:

String url = "http://xxxxxx.net:1900/login";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setReadTimeout(5000);
        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String urlParameters = "username=myusername&password=mypsw";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        String headerName=null;
        String cookie="";
        for (int i=1; (headerName = con.getHeaderFieldKey(i))!=null; i++) {
           if (headerName.equals("Set-Cookie")) {                  
             cookie = con.getHeaderField(i);  
           }
        }
        //print my cookie
        System.out.println("PRIMO COOKIE "+ cookie);

then i need invoke a submit form... i use this code:

String url = "http://xxxx.net:1910/myrequest";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setReadTimeout(5000);
        //add reuqest header
        con.setRequestProperty("Host", "http://xxxx.net:1910");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept","*/*");
        con.setRequestProperty("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4");
        con.setRequestProperty("Cookie", c);  //c is String of my cookie
        con.setRequestProperty("Connection", "keep-alive");

        int responseCode = con.getResponseCode();
        String headerName=null;


        String cookie="";
        for (int i=1; (headerName = con.getHeaderFieldKey(i))!=null; i++) {
           if (headerName.equals("Set-Cookie")) {                  
             cookie = con.getHeaderField(i);  //    <--- the cookie is empty. why?
           }
        }
        System.out.println("SECONDO COOKIE "+ cookie);

why when make my second request required again a login??? why the second cookie is empty?

Best regards

raffys88
  • 11
  • 1

0 Answers0