0

I try to scape some data from a website named alphadraft.com. Unfortunately, I need to authenticate myself to access that data. After searching around for a while I learned that I needed to send a POST request to the website that logs me in.Trough the Chrome DevTools I learned that this should be the should be the post request that logs me in.

After searching around how to do that, I stumbled on this answer and decided to replicate for my purposes. How to send Request payload to REST API in java?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.io.OutputStreamWriter;


public class testasdf {

    public static void main(String[] args) throws IOException, NumberFormatException {

          String line;
            StringBuffer jsonString = new StringBuffer();
            try {

                URL url = new URL("https://alphadraft.com/api/ui/auth/loginuser/");

                //escape the double quotes in json string
                String payload="{email_address : \"(EMAILHERE)\", password : \"(PASSWORDHERE)\"}";
                System.out.println(payload);

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("content-type", "text/plain;charset=UTF-8");
                connection.setRequestProperty("user-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
                writer.write(payload);
                writer.close();
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = br.readLine()) != null) {
                        jsonString.append(line);
                }
                br.close();
                connection.disconnect();
                System.out.println(jsonString.toString());
            } catch (Exception e) {
                    throw new RuntimeException(e.getMessage());
            }
            URL url1 = new URL("https://alphadraft.com/api/ui/contest/getloadinfo/");
            URLConnection con1 = url1.openConnection();
            con1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36");
            con1.connect();
            InputStream is1 =con1.getInputStream();
            BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));


                System.out.println(br1.readLine());
        }
    }

Yet, the error message

{"error": "Failed to login"} {"error": "Not Authenticated"}

still pops up for both my outputs. I would be really grateful if anyone could explain me why this doesn't work.

Community
  • 1
  • 1
TheLegend27
  • 103
  • 3

1 Answers1

0

JSON format requiters quotation marks both for the property name and the value so in your case it should be: String payload="{\"email_address\" : \"(EMAILHERE)\", \"password\" : \"(PASSWORDHERE)\"}";

Yoram
  • 572
  • 6
  • 21
  • Thanks alot, you are absolutly right. The login part now compiles fine.But I still get not authenticated when i try to go on the website, the login is for. Any ideas? – TheLegend27 Feb 07 '16 at 12:50
  • I did actually figure it out now, i needed to get the set-cookie header from the response from the POST request and use it in my GET request for the other page. Thanks anyway. -Thread closed- – TheLegend27 Feb 07 '16 at 20:07