使用HttpUrlConnection访问www.163.com遇到503问题,用设置代理加以解决
一次我使用如下程序连接到网易,意图获取其网站的html文本:
try { String urlPath = "http://www.163.com/"; URL url = new URL(urlPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); File dir = new File("D:\\logs\\"); if (!dir.exists()) { dir.mkdirs(); } File file = new File(dir, "163.txt"); FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[1024 * 8]; int len = -1; while ((len = inputStream.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); fos.close(); }else { System.out.println("download file failed because responseCode="+responseCode); } } catch (Exception e) { e.printStackTrace(); }
但是,实质性代码没有进去,而是进去了else分支,原因是返回码是503。
503是服务器未准备好的意思,但是我用浏览器访问网易是正常的,于是我想有以下可能:
1.网易采用了防爬机制,得在头信息里加入浏览器信息以绕过。
2.未必是网易给我返回的503,中途路由一样可以给我返回。
经测试后,发现头信息加入浏览器信息无效。
这时想浏览器里有代理设置,HttpUrlConnection没有代理怎么可以上网呢,于是在代码开头处加入了代理;
// SetProxy System.setProperty("http.proxyHost", "pkg.proxy.prod.jp.local"); System.setProperty("http.proxyPort", "10080");
然后测试就顺利通过了。
下面是全部代码,供大家参考:
package urlconn; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class DownloadFileTest { public static void main(String[] args) { try { // SetProxy System.setProperty("http.proxyHost", "pkg.proxy.prod.jp.local"); System.setProperty("http.proxyPort", "10080"); String urlPath = "http://www.163.com/"; URL url = new URL(urlPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); File dir = new File("D:\\logs\\"); if (!dir.exists()) { dir.mkdirs(); } File file = new File(dir, "163.txt"); FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[1024 * 8]; int len = -1; while ((len = inputStream.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); fos.close(); }else { System.out.println("download file failed because responseCode="+responseCode); } } catch (Exception e) { e.printStackTrace(); } } }
--2020-03-03--
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2019-03-03 【Canvas与艺术】模拟五个桌球在球桌上进行完全弹性碰撞
2019-03-03 [Canvas与艺术]带尾迹的下雪效果
2019-03-03 【Canvas与诗词】夏日绝句 李清照
2019-03-03 【Canvas与光阑】四条C形色带填满一个圆/环形
2019-03-03 【Canvas与艺术】自制朝阳电脑桌面(1920*1080)
2019-03-03 【Canvas与诗词】绘制黑白纹章,内嵌陶渊明南山诗
2019-03-03 【Canvas与艺术】绘制“Best Product Guaranteed”标志