2017-03-02 94 views
0

我想:如果 输入:<IP-Address> - >输出(EXACT !!!):www.example.comIP到URL到地址

问题:如果我有只是一个网络的IP位址(51.254.201.70)之一网站,并希望找出它是哪个网站,我只是这样得到:70.ip-51-254-201.eu。我想确切www.webmoney.ru

我所了解:

package PACK; 

import java.net.InetAddress; 
import java.net.UnknownHostException; 

public class IPtoHost4 { 

    // Input:www.webmoney.com -> Output: All IP-Adresses of this Page 
public static void main(String[] args) throws UnknownHostException { 


     for (InetAddress addr : InetAddress.getAllByName("www.webmoney.ru")) { 

      System.out.println(addr.getHostAddress() + " : " + getHost(addr)); 
     } 

    // Output: 
    // 5.199.142.158 : www.webmoney.ru 
    // 51.254.201.70 : www.webmoney.ru 
    // 62.210.115.140 : www.webmoney.ru 
    // 88.99.82.144 : www.webmoney.ru 

    // Input:All-IPs -> Output: hostnames 
    String[] ips = { "5.199.142.158", "51.254.201.70", "62.210.115.140", "88.99.82.144" }; 
    for (String i : ips) { 
     InetAddress ip = null; 
     ip = InetAddress.getByName(i); 
     System.out.println(getHost(ip)); 
    } 
    // Output: 
    // z158.zebra.servdiscount-customer.com 
    // 70.ip-51-254-201.eu 
    // 62-210-115-140.rev.poneytelecom.eu 
    // static.144.82.99.88.clients.your-server.de 

} 

static String getHost(InetAddress ip) { 
    String hostName = ""; 
    hostName = ip.getHostName(); 

    return hostName; 

} 

}

+0

你得到这些域名可能是为IPS正确的。你可能会认为你正在查询的域名正在重定向你。除此之外,“webmoney”和“.ru”似乎很可怕,所以要小心。 – Thomas

+0

在一个IP地址上可以有多个域名([虚拟主机](https://en.wikipedia.org/wiki/Virtual_hosting))。 –

+0

嗨。我只有Wireshark的IP地址。我希望发现真正的HTML-网站(DNS服务器和域名对我来说不是很有趣),这些都是我的用户从我的服务器访问的。 –

回答

0

我有一个解决方案,我怎么会找出一个网站的哪些是发送HTML代码301/302用于重定向。但如果我得到HTML代码200我可以确定一个主机名。我想确定网站的全名。

见例如:

public static void main(String[] args) throws IOException { 

    String ip = "78.129.242.2"; 
    URL myURL = new URL("http://" + ip); 

    HttpURLConnection con = (HttpURLConnection) myURL.openConnection(); 
    con.setInstanceFollowRedirects(false); 
    con.connect(); 

    // HTML-Code from a website 
    int responseCode = con.getResponseCode(); 
    System.out.println(responseCode); 

    // HTTP 200 OK 
    if (responseCode == 200) { 
     InetAddress addr = null; 
     addr = InetAddress.getByName(ip); 
     String host = addr.getHostName(); 
     System.out.println(host); 
    } 

    // HTTP 301 oder 302 redirect 
    else if (responseCode == 301 || responseCode == 302) { 
     String location = con.getHeaderField("Location"); 
     System.out.println(location); 
    } else { 
     System.out.println("Bad Web Site" + " ResponceCode: " + responseCode); 
    } 
}