的UnknownHostException

时间:2013-01-03 09:50:55

标签: java exception connection unknown-host

为什么它会抛出UnknownHostException?

    parameters = "user=akirus&pass=1234&version=1";
    String result = excutePost("http://russianimperial.ru/minecraft/loginServer.php", parameters);

  public static String excutePost(String targetURL, String urlParameters) {
      HttpURLConnection connection = null;
      try {
          URL url = new URL(targetURL);
          connection = (HttpURLConnection)url.openConnection();

          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
          connection.setRequestProperty("Content-Language", "en-US");

          connection.setUseCaches(false);
          connection.setDoInput(true);
          connection.setDoOutput(true);

          DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
          wr.writeBytes(urlParameters);
          wr.flush();
          wr.close();

          InputStream is = connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));

          StringBuffer response = new StringBuffer();
          String line;
          while ((line = rd.readLine()) != null) {
              response.append(line);
              response.append('\r');
          }
          rd.close();

          String str1 = response.toString();
          return str1;
     }catch (Exception e) {
         e.printStackTrace();
         return null;
     }finally {
         if (connection != null)
             connection.disconnect();
     }
  }

网址http://russianimperial.ru/minecraft/loginServer.php有效。早些时候它工作得很好。 UnknownHostException投掷在这个地方InputStream is = connection.getInputStream();。 在javadoc中写道connection.getInputStream()可以在协议不支持输入时抛出此异常,但我认为http支持输入。

3 个答案:

答案 0 :(得分:2)

查看JavaDoc

UnknownHostException是

  

抛出以指示主机的IP地址不能   测定

在提出SO问题之前这样做。

答案 1 :(得分:1)

实际上,此代码会产生UnknownHostException。但是看看完整的堆栈跟踪:

java.net.UnknownHostException: updaters
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:523)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:227)
    at sun.net.www.http.HttpClient.New(HttpClient.java:300)
    at sun.net.www.http.HttpClient.New(HttpClient.java:317)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2113)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1367)
    at Host.excutePost(Host.java:43)
    at Host.main(Host.java:14)

当URL连接尝试重定向时,您会收到此异常,并且重定向会转到某些不清楚的“更新程序”。所以它不是'russianimperials.ru',而是重定向它。为什么?它取决于页面, - 您可能也在如何传递参数方面做错了。

答案 2 :(得分:0)

如果你看一下客户端和服务器之间的交换,你会发现:

* Request
GET / HTTP/1.1
Host: russianimperial.ru
User-Agent: Java/1.6.0_26

 * Response
HTTP/1.1 302 Found
Server: nginx/0.8.55
Date: Thu, 03 Jan 2013 11:45:08 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://updaters/tds/go.php?sid=1
Content-Length: 286

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://updaters/tds/go.php?sid=1">here</a>.</p>
<hr>
<address>Apache/2 Server at russianimperial.ru Port 80</address>
</body></html>

如果更改用户代理或未设置用户代理,则不会重定向 但是,由于未知原因,Java用户代理被阻止访问此站点并重定向到非现有主机

更改用户代理,一切都将顺利运行