Answer the question
In order to leave comments, you need to log in
How to get the HTML code of a page in Java in the background?
Hello.
The question is: how do I get the HTML code of a page in the background using Java? So that I enter the URL of the site page into the line, click OK, and he himself gave me the HTML code. No site scripts are of interest. You just need the pulled Ashtiemel code.
Help me please. What Java methods can contribute to this? Quite green in all this.
Answer the question
In order to leave comments, you need to log in
Probably still javascript?
Directly through it, the browser will not give html due to CORS.
Therefore, only using the backend or crutching https://stackoverflow.com/a/18447625, I'm not sure if this will work.
I constantly use this (curved, my hands do not reach to polish) code:
public static String readPageFromUrl(String strURL) throws IOException, InterruptedException {
URL pURL = new URL(strURL);
URLConnection urlCon = (HttpURLConnection) pURL.openConnection();
urlCon.setConnectTimeout(30000000);
urlCon.setReadTimeout(30000000);
urlCon.setRequestProperty("User-Agent", "Mozilla");
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
StringBuilder result = new StringBuilder();
String readLine;
readLine = in.readLine();
while (readLine != null) {
result.append(readLine);
readLine = in.readLine();
}
in.close();
return result.toString();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question