2

I'm trying to get data from site. When I use this code in Intellij IDEA everything works fine, but when I use this code in Android Studio and real device, I get:

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403

Here is my code:

private static final String URL = "http://www.openaip.net/airports?apttype_type_filter=All&country_filter=All&name_filter=EHAM";

private static final String useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
                "AppleWebKit/537.36 (KHTML, like Gecko) " +
                "Chrome/52.0.2743.116 Safari/537.36";

Document document = Jsoup.connect(URL)
                        .userAgent(useragent)
                        .ignoreContentType(true)
                        .timeout(5000)
                        .execute() //  <-- on this line I get error
                        .parse();

All information that I have found is only about "userAgent()" method, but It didn't help.

UPD: Sorry, it was my bad in question. Correct url: http://www.openaip.net/airports?apttype_type_filter=All&country_filter=All&name_filter=EHAM, but I still have the same problem.

Error: org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=http://www.openaip.net/airports?apttype_type_filter=All&country_filter=All&name_filter=EHAM

3 Answers 3

4

I've added the missing header in the request and I was able to retrieve the website content, the code I've used is:

Document document = Jsoup.connect(URL)
                            .userAgent("Mozilla")
                            .header("Accept", "text/html")
                            .header("Accept-Encoding", "gzip,deflate")
                            .header("Accept-Language", "it-IT,en;q=0.8,en-US;q=0.6,de;q=0.4,it;q=0.2,es;q=0.2")
                            .header("Connection", "keep-alive")
                            .ignoreContentType(true)
                            .get();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you alot! You saved my time!
This solution doesn't work for masterclass.com. Can you suggest anything?
1

It's not a problem with Android.

Try to access the URL in your browser, it won't work either.

Be sure to verify that it is the correct URL you are trying to access.

1 Comment

Sorry, it was my bad in question. Correct url: openaip.net/…, but I still have the same problem.
0

I dont know if you guys had already sloved this problem. I wasted 5 hours solving the damn thing. The "catch" was in the user-agent, you need to put user agents for mobile platform like this:

                        Jsoup.connect(url)
                        .method(Connection.Method.GET)
                    --->.userAgent("Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.5414.117 Mobile Safari/537.36")
                        .referrer("https://www.google.com")
                        .header("accept", "*/*")
                        .timeout(3000)
                        .header("content-type", "text/plain;charset=UTF-8")
                        .get());

List of latest user-agents for android can be found here: https://www.whatismybrowser.com/guides/the-latest-user-agent/android

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.