0

为什么会抛出 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 支持输入。

4

3 回答 3

2

实际上,此代码会产生 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”,而是它所做的重定向。为什么?这取决于一个页面,你也可能在传递参数的方式上做错了。

于 2013-01-03T10:03:43.197 回答
1

看看JavaDoc

UnknownHostException 是

抛出表示无法确定主机的 IP 地址

在问一个 SO 问题之前先做。

于 2013-01-03T09:52:45.103 回答
1

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

* 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 用户代理被此站点阻止并重定向到不存在的主机

更换用户代理,一切顺利

于 2013-01-03T11:53:53.127 回答