如何在Java中阻止403 HTTP错误代码?

我使用简单的代码获取http://www.ip-adress.com的html,但它显示错误http代码403.
我在程序中像google.com这样的其他网站试试,它可以工作.我也可以在浏览中打开www.ip-adress.com,为什么我不能在java程序中使用它.

 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)
最佳答案
这是您需要的行

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");

参考this

转载注明原文:如何在Java中阻止403 HTTP错误代码? - 代码日志