I have a problem with Jsoup library, which when I try to make the connection to a certain page, it simply does not return any value from the connection.
public static void main(String[] args) {
try{
Document doc = Jsoup.connect("http://pt.stackoverflow.com/").get();
//Pegando elemento das perguntas
Elements elements = doc.select("a.question-hyperlink");
System.out.println("O titulo da página é: "+doc.title());
//exibindo titulo da pergunta
for(int i = 0; i <elements.size(); i++){
System.out.println(elements.get(i).text());
}
}catch(Exception e){
System.out.println("Erro "+ e);
}
}
By coincidence I tested it with Stack Overflow and it gave the same problem.
Netbeans IDE Return:
Erro: org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=http://pt.stackoverflow.com/
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.282s
Finished at: Sun May 15 13:26:41 BRT 2016
Final Memory: 5M/109M
------------------------------------------------------------------------
I do not know if this influences something, but the project type is Maven.
@EDIT
I was able to resolve the problem by adding the following method to the connection.
Document doc = Jsoup.connect("http://pt.stackoverflow.com/")
.userAgent("Mozilla").get();