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

/*add--2021年2月4日

* java.io.IOException: Server returned HTTP response code: 403 for URL
* 服务器端禁止抓取
* 设置User-Agentbai欺骗服务器
* */
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)"); //防止报403错误。
urlConnection.connect();

final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));// 读取网页全部内容

作者:Kaspar_Choo
         
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/kaspar/p/14376545.html