Java Eclipse中的未知主机异常

Java Eclipse中的未知主机异常,java,eclipse,http,https,proxy,Java,Eclipse,Http,Https,Proxy,当我编译并运行下面的代码时,会抛出未知主机异常 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

当我编译并运行下面的代码时,会抛出未知主机异常

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中运行上述代码。

您可以在启动JVM时设置所需的属性。在eclipse
运行->运行配置->参数选项卡中,设置VM参数

-Dhttp.proxyHost=Your/proxy/server.com  -Dhttp.proxyPort=80
您可以使用由网络设置定义的默认代理

System.setProperty("java.net.useSystemProxies", "true");
或在打开连接之前使用您的代理:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Proxy/address", 8080/*port*/));
URL url = new URL("https://www.google.co.in/?gfe_rd=cr&ei=URzCVNmFKIiBoAPlpoD4CA&gws_rd=ssl#q=jbutton");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));

如果您支持代理,您将面临此问题。无论何时使用浏览器,是否设置了任何代理?如果是,您还需要为此程序进行配置

此外,您还可以尝试在eclipse运行配置中设置代理:

-DproxySet=true -DproxyHost=<proxy host> -DproxyPort=<port> JavApp
-DproxySet=true-DproxyHost=-DproxyPort=JavApp

@Steffen谢谢。可能是我在错误的位置(打开连接后)分配代理时出错了。