How can I prevent a 403 HTTP error code in Java?
http, http-headers, java
Solution
This is the line you required
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
refer this
Problem
I use simple code to get html for http://www.ip-adress.com, but it shows error http code 403. I try it in other website like google.com in program, it can work. i can also open www.ip-adress.com in browse, why i can't use it in java program. ``` public class urlconnection { public static void main(String[] args) { StringBuffer document = new StringBuffer(); try { URL url = new URL("http://www.ip-adress.com"); URLConnection conn = url.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) document.append(line + " "); reader.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(document.toString()); } } java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.ip-adress.com/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at urlconnection.main(urlconnection.java:14) ```