Example usage for java.net InetAddress getAllByName

List of usage examples for java.net InetAddress getAllByName

Introduction

In this page you can find the example usage for java.net InetAddress getAllByName.

Prototype

public static InetAddress[] getAllByName(String host) throws UnknownHostException 

Source Link

Document

Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.

Usage

From source file:GetAllIP.java

public static void main(String[] args) throws Exception {
    InetAddress[] addr = InetAddress.getAllByName("www.java2s.com");
    for (int i = 0; i < addr.length; i++)
        System.out.println(addr[i]);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress[] addr = InetAddress.getAllByName(args[0]);
    for (int i = 0; i < addr.length; i++)
        System.out.println(addr[i]);
}

From source file:MainClass.java

public static void main(String[] args) {

    try {//from  w ww.  j  a v a  2s .  c o m
        InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
        for (int i = 0; i < addresses.length; i++) {
            System.out.println(addresses[i]);
        }
    } catch (UnknownHostException e) {
        System.out.println("Could not find www.apple.com");
    }

}

From source file:MainClass.java

public static void main(String args[]) {
    try {/*w  w w.  ja v  a2  s  .  co m*/

        // Get an address
        InetAddress ias[] = InetAddress.getAllByName("www.google.com");
        for (int i = 0; i < ias.length; i++) {
            System.out.println(ias[i].getHostName());
            System.out.println(ias[i].getHostAddress());
            byte bytes[] = ias[i].getAddress();
            for (int j = 0; j < bytes.length; j++) {
                if (j > 0)
                    System.out.print(".");
                if (bytes[j] >= 0)
                    System.out.print(bytes[j]);
                else
                    System.out.print(bytes[j] + 256);
            }
            System.out.println("");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    try {//from  w  ww .  j a v a2 s  .  co m

        int timeout = 2000;
        InetAddress[] addresses = InetAddress.getAllByName("www.java2s.com");
        for (InetAddress address : addresses) {
            if (address.isReachable(timeout))
                System.out.printf("%s is reachable%n", address);
            else
                System.out.printf("%s could not be contacted%n", address);
        }
    } catch (Exception e) {
        System.out.printf("Unknown host: %s%n", "www.java2s.com");
    }
}

From source file:InetAddressTest.java

public static void main(String[] args) {
    try {/*  ww  w  .j av a  2  s  .  co  m*/
        if (args.length > 0) {
            String host = args[0];
            InetAddress[] addresses = InetAddress.getAllByName(host);
            for (InetAddress a : addresses)
                System.out.println(a);
        } else {
            InetAddress localHostAddress = InetAddress.getLocalHost();
            System.out.println(localHostAddress);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.cloudkit.relaxation.HttpClientTest.java

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

    InetAddress[] addresss = InetAddress.getAllByName("google.com");
    for (InetAddress address : addresss) {

        System.out.println(address);

    }/*from w ww  . j a v a 2  s.  c  o m*/

    CloseableHttpClient httpclient = HttpClients.createDefault();

    String __VIEWSTATE = "";
    String __EVENTVALIDATION = "";

    HttpGet httpGet = new HttpGet("http://query.customs.gov.cn/MNFTQ/MRoadQuery.aspx?" + Math.random() * 1000);
    httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    httpGet.setHeader("Accept-Encoding", "gzip, deflate, sdch");
    httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
    httpGet.setHeader("Cache-Control", "no-cache");
    // httpGet.setHeader("Connection", "keep-alive");
    httpGet.setHeader("Host", "query.customs.gov.cn");
    httpGet.setHeader("Pragma", "no-cache");
    httpGet.setHeader("Upgrade-Insecure-Requests", "1");
    httpGet.setHeader("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");

    HttpClientContext context = HttpClientContext.create();
    // CloseableHttpResponse response1 = httpclient.execute(httpGet, context);
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    // Header[] headers = response1.getHeaders(HttpHeaders.CONTENT_TYPE);
    // System.out.println("context cookies:" + context.getCookieStore().getCookies());
    // String setCookie = response1.getFirstHeader("Set-Cookie").getValue();
    // System.out.println("context cookies:" + setCookie);

    try {
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        // do something useful with the response body and ensure it is fully consumed

        String result = IOUtils.toString(entity1.getContent(), "GBK");
        // System.out.println(result);

        Matcher m1 = Pattern.compile(
                "<input type=\\\"hidden\\\" name=\\\"__VIEWSTATE\\\" id=\\\"__VIEWSTATE\\\" value=\\\"(.*)\\\" />")
                .matcher(result);
        __VIEWSTATE = m1.find() ? m1.group(1) : "";
        Matcher m2 = Pattern.compile(
                "<input type=\\\"hidden\\\" name=\\\"__EVENTVALIDATION\\\" id=\\\"__EVENTVALIDATION\\\" value=\\\"(.*)\\\" />")
                .matcher(result);
        __EVENTVALIDATION = m2.find() ? m2.group(1) : "";

        System.out.println(__VIEWSTATE);
        System.out.println(__EVENTVALIDATION);

        /*
        File storeFile = new File("D:\\customs\\customs"+ i +".jpg");
        FileOutputStream output = new FileOutputStream(storeFile);
        IOUtils.copy(input, output);
        output.close();
        */
        EntityUtils.consume(entity1);
    } finally {
        response1.close();
    }

    HttpPost httpPost = new HttpPost(
            "http://query.customs.gov.cn/MNFTQ/MRoadQuery.aspx?" + Math.random() * 1000);
    httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    httpPost.setHeader("Accept-Encoding", "gzip, deflate");
    httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
    httpPost.setHeader("Cache-Control", "no-cache");
    // httpPost.setHeader("Connection", "keep-alive");
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.setHeader("Cookie", "ASP.NET_SessionId=t1td453hcuy4oqiplekkqe55");
    httpPost.setHeader("Host", "query.customs.gov.cn");
    httpPost.setHeader("Origin", "http://query.customs.gov.cn");
    httpPost.setHeader("Pragma", "no-cache");
    httpPost.setHeader("Referer", "http://query.customs.gov.cn/MNFTQ/MRoadQuery.aspx");
    httpPost.setHeader("Upgrade-Insecure-Requests", "1");
    httpPost.setHeader("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
    nvps.add(new BasicNameValuePair("__EVENTVALIDATION", __EVENTVALIDATION));
    nvps.add(new BasicNameValuePair("ScrollTop", ""));
    nvps.add(new BasicNameValuePair("__essVariable", ""));
    nvps.add(new BasicNameValuePair("MRoadQueryCtrl1$txtManifestID", "5100312462240"));
    nvps.add(new BasicNameValuePair("MRoadQueryCtrl1$txtBillNo", "7PH650021105"));
    nvps.add(new BasicNameValuePair("MRoadQueryCtrl1$txtCode", "a778"));
    nvps.add(new BasicNameValuePair("MRoadQueryCtrl1$btQuery", "   "));
    nvps.add(new BasicNameValuePair("select", ""));
    nvps.add(new BasicNameValuePair("select1", ""));
    nvps.add(new BasicNameValuePair("select2", ""));
    nvps.add(new BasicNameValuePair("select3", ""));
    nvps.add(new BasicNameValuePair("select4", ""));
    nvps.add(new BasicNameValuePair("select5", "??"));
    nvps.add(new BasicNameValuePair("select6", ""));
    nvps.add(new BasicNameValuePair("select7", ""));
    nvps.add(new BasicNameValuePair("select8", ""));

    httpPost.setEntity(new UrlEncodedFormEntity(nvps, "GBK"));

    CloseableHttpResponse response2 = httpclient.execute(httpPost);

    try {
        System.out.println(response2.getStatusLine());
        HttpEntity entity2 = response2.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        // System.out.println(entity2.getContent());
        System.out.println(IOUtils.toString(response2.getEntity().getContent(), "GBK"));

        EntityUtils.consume(entity2);
    } finally {
        response2.close();
    }

}

From source file:Main.java

public static Map<String, Object> getDomainIp(String _dormain) {
    Map<String, Object> map = new HashMap<String, Object>();
    long start = 0;
    long end = 0;
    String time = null;/*from ww w  . j  av  a2 s . c om*/
    InetAddress[] remoteInet = null;
    try {
        start = System.currentTimeMillis();
        remoteInet = InetAddress.getAllByName(_dormain);
        if (remoteInet != null) {
            end = System.currentTimeMillis();
            time = (end - start) + "";
        }
    } catch (UnknownHostException e) {
        end = System.currentTimeMillis();
        time = (end - start) + "";
        remoteInet = null;
        e.printStackTrace();
    } finally {
        map.put("remoteInet", remoteInet);
        map.put("useTime", time);
    }
    return map;
}

From source file:com.joyent.manta.http.ShufflingDnsResolver.java

@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
    final InetAddress[] addresses = InetAddress.getAllByName(host);
    shuffle(addresses);/*from  w w  w. ja v  a  2  s  .  c  o  m*/

    return addresses;
}

From source file:Main.java

protected static void obtainHostInfo() {
    HOST_IPADDRESS = "127.0.0.1";
    HOST_NAME = "";

    try {/*from  w ww . ja v  a2s.c  o m*/
        InetAddress primera = InetAddress.getLocalHost();
        String hostname = InetAddress.getLocalHost().getHostName();

        if (!primera.isLoopbackAddress() && !primera.getHostName().equalsIgnoreCase("localhost")
                && primera.getHostAddress().indexOf(':') == -1) {
            // Get it without delay!!

            //System.out.println ("we have it!");
            HOST_IPADDRESS = primera.getHostAddress();
            HOST_NAME = hostname;
            //System.out.println (hostname + " IP " + HOST_IPADDRESS);
            return;
        }

        // Get it by slow way ...
        InetAddress[] familia = InetAddress.getAllByName(hostname);

        int netto = 1;
        for (int aa = 0; aa < familia.length; aa++) {
            // System.out.println ("Networko " + netto++);
            InetAddress laAdd = familia[aa];
            String ipstring = laAdd.getHostAddress();
            hostname = laAdd.getHostName(); // don't know if it can change, probably not

            // System.out.println ("checking : " + hostname + " IP " + ipstring);
            if (laAdd.isLoopbackAddress())
                continue;
            if (hostname.equalsIgnoreCase("localhost"))
                continue;
            if (ipstring.indexOf(':') >= 0)
                continue;

            // System.out.println ("this pass!");
            HOST_IPADDRESS = ipstring;
            HOST_NAME = hostname;
            break;
        }
    } catch (Exception e) {
    }
}