I'm dveloping a small test to download an apk from apkpure using java. The url i have return a 403. But on browser, it works fine. All solutions i tried did not work.
URL url = new URL(TheUrl);
try (InputStream in = url.openStream()) {
Files.copy(in, Paths.get(apkPathInFolder), StandardCopyOption.REPLACE_EXISTING);
System.out.println("test ok");
} catch (IOException e) {
// handle exception
System.out.println(e);
}
How could i handle it please ?
Thanks
An example of url i have as input:
CodePudding user response:
The default Java user-agent is blocked by some online services. You need to set the User-Agent
header to something else:
URL url = new URL(httpUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("User-Agent", "my-agent");
httpURLConnection.connect();
try (InputStream in = httpURLConnection.getInputStream()) {
CodePudding user response:
you can get the header from browser and set it in you code.
if you use chrome browser, open the dev tools in your request page.