Rumah  >  Artikel  >  pangkalan data  >  代理设置问题引起的UnKnownHostException Jsoup执行execute()错

代理设置问题引起的UnKnownHostException Jsoup执行execute()错

WBOY
WBOYasal
2016-06-07 16:32:251596semak imbas

执行下面的语句时抛出了UnKnownHostException异常: Response response = Jsoup.connect(path) .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21") .timeout(10000) .execute(); [09-

javaee-2013090901

执行下面的语句时抛出了UnKnownHostException异常:

Response response = Jsoup.connect(path)
                .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
                .timeout(10000)
                .execute();

[09-09 08:26:13.822] java.net.UnknownHostException: www.baidu.com
[09-09 08:26:13.823] 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
[09-09 08:26:13.823] 	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)

如果是通过使用代理上网的,应该是和代理的设置有关,需要在操作系统或者程序中设置好代理。(假设这里的代理服务器是:192.168.10.20:8080)

如果是请求一个url,直接设置一下系统的网络代理就可了额,Windows下在Internet选项里面设置代理即可:

20130909-proxy01

而在Linux下的设置入下:

在 /etc/profile文件中设置环境变量,分别制定HTTP,HTTPS,FTP协议使用的代理服务器地址:

http_proxy=192.168.10.20:8080
https_proxy=192.168.10.20:8080
ftp_proxy=192.168.10.20:8080
no_proxy=192.168.10.  # 访问局域网地址(192.168.10.0/24网段)时不使用代理
export http_proxy https_proxy ftp_proxy no_proxy

配置好之后保存,退出,注销重新登录后即可生效

Java程序中设置代理:

而这样配置之后如果还是会出现UnKnownHostException,很可能是系统配置的代理没有生效,可以在程序中添加如下代理试试:

System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.proxyHost", "192.168.10.20");
System.getProperties().put("http.proxyPort", "8080");
// System.getProperties().put("http.proxyUser", user);
// System.getProperties().put("http.proxyPassword", password);
System.getProperties().put("http.nonProxyHosts", "localhost|127.0.0.1");
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn