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"));// 读取网页全部内容

posted @ 2021-02-05 10:58  Kaspar_Choo  阅读(2537)  评论(0编辑  收藏  举报