Home > Blockchain >  403 error in downloading with URL but works fine in browsers
403 error in downloading with URL but works fine in browsers

Time:09-27

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:

https://download.apkpure.com/b/APK/Y29tLnRlbmNlbnQuaWdfMTU1MjNfYmFhM2U4Njk?_fn=UFVCRyBNT0JJTEUgUkVTSVNUQU5DRV92MS42LjBfYXBrcHVyZS5jb20uYXBr&as=1e5ab5a5ef235ad0674f6ed4f7ff59576150d1cd&ai=33533981&at=1632686421&_sa=ai,at&k=06ee0e1f9f5964c4edc23905822a9c8861537455&_p=Y29tLnRlbmNlbnQuaWc&c=2|GAME_ACTION|ZGV2PVBST1hJTUElMjBCRVRBJnQ9YXBrJnM9MTEzMjQ5NjAwMiZ2bj0xLjYuMCZ2Yz0xNTUyMyZodD1lMDE&ht=e01

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.

  • Related