0

why it throw 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();
     }
  }

url http://russianimperial.ru/minecraft/loginServer.php is valid. earlier it work's great. UnknownHostException throwed in this place InputStream is = connection.getInputStream();. in javadoc written that connection.getInputStream() can throw this Exception when protocol don't support input, But I think http support input.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Akirus
  • 108
  • 2
  • 11

3 Answers3

2

Indeed, this code produces UnknownHostException. But have a look at the full stack trace:

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)

You get this exception when URL connection tries to redirect, and redirect goes to some unclear 'updaters'. So it's not 'russianimperials.ru', but the redirect it makes. Why? It depends on a page,- you might also be doing something wrong on how you pass parameters.

Art Licis
  • 3,619
  • 1
  • 29
  • 49
1

Look at JavaDoc:

UnknownHostException is

Thrown to indicate that the IP address of a host could not be determined

Do it before asking a SO question.

jlordo
  • 37,490
  • 6
  • 58
  • 83
1

If you look at the exchange between the client and the server, you find this :

* 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>

If you change the user agent, or set no user agent, there is no redirection But, for an unknown reason, Java user agent are blocked from this site and redirected toward a non existing host

Change the user agent, and all will run smoothly

Grooveek
  • 10,046
  • 1
  • 27
  • 37
  • How to look between exchange, can you please share how do you find above Request and Response? – Nikhil Joshi Apr 10 '14 at 13:13
  • 1
    You have several options depending on what HTTP client you use. In Chrome or Firefox Developer tools , you can inspect headers. You can also use the `curl` tool with `curl -v http://website.to.test -o /dev/null` on Linux. Another tool on Linux is the GUI tool `wireshark` (or `tcpdump` for its command-line couterpart). All these tools will let you inspect the headers in your requests – Grooveek Apr 11 '14 at 07:19
  • Thank you Grooveek for details! – Nikhil Joshi Apr 11 '14 at 07:22