0

Imagine a site example.com

There is some page which is allowed to see for users who logged in (example.com/page). I want to get source code of that page.

Then i found out that i should use cookies of my browser and send them somehow to site. After sending cookies, i need to read code of website.

How to do it? What I have now :

public class HtmlCodeManager {

    public static ArrayList<String> cookies = new ArrayList<String>();

    public static void read() {
        cookies.add("cookie1name=cookie1value");
        cookies.add("cookie2name=cookie2value");

        URL url;
        String postData = "WHAT DATA SHOULD BE THERE?";
        try {
            url = new URL("https://example.com/page");

            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            for(String cookie : cookies) {
                con.addRequestProperty("Cookie", cookie);
            }
            con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
            con.connect();

            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
            out.write(postData);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Yurets
  • 3,999
  • 17
  • 54
  • 74
  • If this is not for homework, then you can save yourself a lot of grief by using a library instead of trying to write all the code yourself. See this answer, for example: http://stackoverflow.com/questions/2586975/how-to-use-curl-in-java – Solomon Slow Jan 30 '17 at 03:10

1 Answers1

0

you can add cookies in your response, Like responseObj.addCookies(cookie);

Sunoo
  • 1
  • 6