简体   繁体   中英

Java - doing post request to login page

Im trying to log in to https://flow.polar.com/login page using java. I did the post request with 'email' and 'password' value. I should be redirected to page to authorize geting data from the user, but insted im getting 200 response and im redirecting to main page ( https://flow.polar.com ). I was checking all the values in the browser request option,as I log in normally, but still got this bug. My question is, is the anything im missing to log in, or is there a method to click sign in button?

I want to also add that when I provide wrong email or password im getting 400 response. So everything seems to work fine except im redirecting to wrong page :(

Here is my code:

URL obj = new URL(url);
    HttpsURLConnection conn1 = (HttpsURLConnection) obj.openConnection();

    // Acts like a browser
    conn1.setUseCaches(false); // just going to main page instead of logging!!!
    conn1.setRequestMethod("POST");
    conn1.setRequestProperty("Host", "flow.polar.com");
    conn1.setRequestProperty("User-Agent", USER_AGENT);
    conn1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn1.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    conn1.setRequestProperty("Accept-Language", "pl,en-US;q=0.7,en;q=0.3");
    conn1.setRequestProperty("Connection", "keep-alive");
    conn1.setRequestProperty("Referer", "https://flow.polar.com/login?n=D6FQQAAAAAAAAAAACXDM2CSAIAIABYDX3GB5XJTBP5IPEIESYYNNTUUOLAD6JXLR7H5G4EKE2WFJJ4MIOOLP54XGF6GJ4Q5T2G7HFWFJR7TUVNPDSEJLO6AKWH3WG3IIFTRKIJCZKVEAKMCJDSUPYZSUV2WYMDFU5CPBOIZJBGZGCAAAAA%3D%3D%3D%3D%3D%3D");

    conn1.setDoOutput(true);
    conn1.setDoInput(true);

    // Send post request
    DataOutputStream wr = new DataOutputStream(conn1.getOutputStream());
    wr.writeBytes(postParams);
    System.out.println(postParams);

    wr.flush();
    wr.close();

    int responseCode = conn1.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + postParams);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in =
            new BufferedReader(new InputStreamReader(conn1.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    System.out.println("Response: ");
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

Okey. I got it working. The problem was that I didnt POST ALL input fileds. I forgot about hidden ones. Here is working code:

 Connection.Response response1 =
            Jsoup.connect("https://flow.polar.com/login?n=XXX")
                    .userAgent(USER_AGENT)
                    .timeout(10 * 1000)
                    .method(Method.POST)
                    .data("email", "xxx@gmail.com")
                    .data("password", "passwd")
                    .data("returnUrl", "/?n=XXX")
                    .followRedirects(true)
                    .execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM