一次我使用如下程序连接到网易,意图获取其网站的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--

使用HttpUrlConnection访问www.163.com遇到503问题,用设置代理加以解决的更多相关文章

  1. vue访问外部接口设置代理,解决跨域(vue-cli3.0)

    vue-cli3.0搭建的项目,平时访问内部接口配置了拦截器,今天需要调用天气预报的外部接口,发现跨域问题,通过配置代理解决. 1.在vue.config.js中配置代理 module.exports ...

  2. 访问网页时提示的503错误信息在IIS中怎么设置

    访问网页时提示的503错误信息在IIS中怎么设置 503是一种常见的HTTP状态码,出现此提示信息的原因是由于临时的服务器维护或者过载,服务器当前无法处理请求则导致了访问网页时出现了503错误.那么当 ...

  3. java成神之——HttpURLConnection访问api

    HttpURLConnection 访问get资源 访问post资源 访问Delete资源 获取状态码 结语 HttpURLConnection 访问get资源 HttpURLConnection c ...

  4. HttpUrlConnection访问Servlet进行数据传输

    建立一个URL url = new URL("location"); 建立 httpurlconnection :HttpUrlConnection httpConn = (Htt ...

  5. HttpURLConnection访问网络

    HttpURLConnection是一个抽象类,获取HttpURLConnection对象HttpURLConnection urlConnection=new URL(http://www.baid ...

  6. cxf设置代理访问webservice接口

    由于业务上的需要,需要访问第三方提供的webservice接口,但由于公司做了对外访问的限制,不设置代理是不能外网的,如果使用http设置代理访问外网还是比较容易的,但使用cxf有点不知道从哪里入手. ...

  7. webservice axis2客户端设置代理方法(公司网络通过代理访问时)

    webservice axis2客户端设置代理方法(公司网络通过代理访问时)   UploadProcessInServiceStub stub = new UploadProcessInServic ...

  8. HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理

    原文地址:http://blog.csdn.net/cdefg198/article/details/8315438 万能框架:http://www.sufeinet.com/forum.php?mo ...

  9. crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理

    在工作中要用到android,然后进行网络请求的时候,打算使用httpClient. 总结一下httpClient的一些基本使用. 版本是4.2.2. 使用这个版本的过程中,百度很多,结果都是出现的o ...

随机推荐

  1. find the lowest number location

    before #设定路径列表Path def find_path2(heightmap, x, y, water_level=557,path=[]): #global path #设定坐标 右0 左 ...

  2. 对象原型之__proto__

    对象都会有一个__proto__指向构造函数的prototype原型对象,对象之所以能够使用构造函数的prototype原型对象的方法,就是因为有__proto__原型的存在.       funct ...

  3. Linux学习日志第一天——基础命令①

    文章目录 前言 命令的作用及基本构成 关于路径 命令 ls (list) 命令 pwd (print working directory) 命令cd (change directory) 命令 mkd ...

  4. 使用 VMware Workstation Pro让 PC 提供云桌面服务——学习笔记(三)

    目标 当在前面两篇博客后,我们已经创建了一个能当服务器的虚拟机,这时我们需要通过复制虚拟机来让创建更多虚拟机 操作步骤 1.创建克隆 这里主要是VMware软件的操作 虚拟机->管理->克 ...

  5. 【AHOI2009】 维护序列 - 线段树

    题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一 ...

  6. 图论算法(四)Dijkstra算法

    最短路算法(三)Dijkstra算法 PS:因为这两天忙着写GTMD segment_tree,所以博客可能是seg+图论混搭着来,另外segment_tree的基本知识就懒得整理了-- Part 1 ...

  7. String、StringBuilder、StringBuffer三者的区别

    StringBuffer.StringBuilder和String都可以用来代表字符串.String类是不可变类,任何对String的改变都会引发新的String对象的生成:StringBuffer. ...

  8. unsigned char printf 如何输出

    参考链接:https://blog.csdn.net/m0_37362454/article/details/88639668 #include <stdio.h> int main() ...

  9. latex:在公式之中和公式之间插入说明文字和标点符号

    在公式之中和公式之间插入说明文字和标点符号,主要使用 \intertext{文本} \shortintertext{文本} \text{文本} 这三个命令 代码: \begin{align*}x^{2 ...

  10. #企业项目实战 .Net Core + Vue/Angular 分库分表日志系统六 | 最终篇-通过AOP自动连接数据库-完成日志业务

    教程预览 01 | 前言 02 | 简单的分库分表设计 03 | 控制反转搭配简单业务 04 | 强化设计方案 05 | 完善业务自动创建数据库 06 | 最终篇-通过AOP自动连接数据库-完成日志业 ...