2017-03-02 93 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); 
    } 
}