【问题标题】:Unknown Host Exception in Eclipse [duplicate]Eclipse中的未知主机异常[重复]
【发布时间】:2015-03-22 08:23:37
【问题描述】:

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

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

【问题讨论】:

  • @Steffen 谢谢。可能我在错误的地方分配代理时犯了一个错误(打开连接后)。

标签: java eclipse http https proxy


【解决方案1】:

您可以在启动 JVM 时设置所需的属性。在eclipseRun-> run configuration-> Argument Tab中,设置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()));

【讨论】:

    【解决方案2】:

    如果您使用代理,您将面临这个问题。每当您使用浏览器时,您是否设置了任何代理?如果是,您也需要为此程序进行配置。

    另外,你可以尝试在eclipse运行配置中设置代理:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 2011-05-24
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      相关资源
      最近更新 更多