Я хочу прочитать js файл как строку из url https://www.hellobank.fr/rsc/contrib/script/hb2/js/app/pages/video-bienvenue.js
Мой код:
StringBuilder sb = new StringBuilder();
URL url = new URL(jsUrl);
URLConnection cnt = url.openConnection();
InputStream stream = new URL(jsUrl).openStream();
if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
stream = new GZIPInputStream(stream);
}
// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line;
while ((line = in .readLine()) != null) {
sb.append(line).append("\n");
} in .close();
Вместо реального содержимого js я получил пустой html файл:
<html>
<body>
<script type="text/javascript">
window.location = "/fr/systeme/maintenance?&ID=12646317151369496393&eq=LSM2TDBW";
</script>
</body>
</html>
Может ли кто-нибудь помочь мне понять, в чем проблема с этой js-ссылкой и как ее правильно читать?
Вы должны отправить GET
, как и в своем браузере. Вы можете прочитать о том, как это сделать здесь.
И ваш код будет примерно таким же, как в примере:
URL obj = new URL("https://www.hellobank.fr/rsc/contrib/script/hb2/js/app/pages/video-bienvenue.js");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString()); //here is the content of the file you need
или быть ближе к вашему коду, это будет примерно так:
String jsUrl = "https://www.hellobank.fr/rsc/contrib/script/hb2/js/app/pages/video-bienvenue.js";
URL obj = new URL(jsUrl);
HttpURLConnection cnt = (HttpURLConnection) obj.openConnection();
cnt.setRequestMethod("GET");
cnt.setRequestProperty("User-Agent", "Mozilla/5.0");
InputStream stream = cnt.getInputStream();
if ("gzip".equalsIgnoreCase(cnt.getHeaderField("Content-Encoding"))) {
stream = new GZIPInputStream(stream);
}
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString()); //here is the content of the file you need