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

设置完成,重新启动程序。



猜你喜欢

转载自blog.csdn.net/cling_snail/article/details/80858897