当我编译并运行下面的代码时,我抛出未知主机异常。
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
String result="";
// Create a URL for the desired page
URL url = new URL("https://www.google.co.in/?gfe_rd=cr&ei=URzCVNmFKIiBoAPlpoD4CA&gws_rd=ssl#q=jbutton");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
result += str;
}
in.close();
System.out.println(result);
}
}
如何为上述代码(Https 和 HTTP)设置代理?我在 Eclipse Kepler 中运行上述代码。
如果您使用代理,您将面临此问题。每当您使用浏览器时,您是否设置了代理?如果是,您还需要为此程序进行配置。
另外,您可以尝试在 eclipse 运行配置中设置代理:
-DproxySet=true -DproxyHost=<proxy host> -DproxyPort=<port> JavApp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)