G
G
George2019-09-12 01:37:46
Java
George, 2019-09-12 01:37:46

How to correctly get Cookies in Java?

Hello! I have a code that correctly receives cookies from the site and saves it in a file, I run it from the development environment:

String url = "https://www.avito.ru";
Map<String, String> cookies = Jsoup.connect(url).execute().cookies();
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
                out.writeObject(cookies);
                System.out.println("Get cookies from: " + url + " and write into: " + file.getName());
                System.out.println(cookies);
                out.close();

But when I upload and run it on the Heroku server (only now I temporarily store cookies in the handler), I get an error in the logs.
It occurs at the .execute() step.
Code uploaded to Heroku:
try {
                            Map <String, String> cookies = Jsoup.connect(url)
                                    .execute()
                                    .cookies();
                            cookieHandler.setAvitoCookies(cookies);
                            System.out.println("> AVITO COOKIE:" + cookies); 
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

Heroku error log:
org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=https://www.avito.ru

What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
protven, 2019-09-12
@protven

The problem is you. There is a clear error in the log. Google what http 403 means and think about why a site that is constantly trying to parse returned it to a request from your robot.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question