IP到URL到地址

时间:2017-03-02 11:48:48

标签: java ip ip-address hostname

我想:  如果输入:<IP-Address> - &gt;输出(EXACT !!!):www.example.com

问题:如果我只有一个网站的IP地址(51.254.201.70)并想知道它是哪个网站,我只能这样:70.ip-51-254-201 。欧洲联盟。我想得到准确的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;

}

}

1 个答案:

答案 0 :(得分:0)

我有一个解决方案,我如何找到一个发送HTML代码301/302重定向的网站。但如果我获得 HTML Code 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);
    }
}