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

這樣就可以訪問了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM