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();
}
}
}