在調用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)");
這樣就可以訪問了。