java.io.IOException: Server returned HTTP response code: 403 for URL

在调用api接口做测试的时候出现java.io.IOException: Server returned HTTP response code: 403 for URL

因为服务器的安全设置不接受Java程序作为客户端访问,解决方案是设置客户端的User Agent


url = new URL("http://physics.whu.edu.cn/show.asp?id=278");
            HttpURLConnection connection = (HttpURLConnection) url.
                openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

这样就可以访问了。
原文地址:https://www.cnblogs.com/xijieblog/p/4540026.html