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

这样就可以访问了。
posted @ 2015-04-24 15:05  xijie0101  阅读(10676)  评论(1编辑  收藏  举报