I need to open a connection to an URL address and to read html with StringBuffer. However when i try to establish connection i get file not found exception 404. When i write this address in any browser on Windows or Android, address can be accessed anytime. I do not understand why this happens or is there any workaround.
URL meteo = new URL("http://meteo.bg/bg/osnOpasni");
HttpURLConnection connection = (HttpURLConnection) meteo.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Accept", "text/html, */*");
connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");
int responseCode = connection.getResponseCode();
If i use same code for any other URL - it works and the ResponseCode is 200. Is there anything i am missing???
I try de URL on a browser with DevTools and the 404 error appears in the first place using the F12 Network analisis:
Seems like the URL is answering with a 404 error:
No error on your code the error is there in the website.