集册 Java实例教程 获取网络Ip地址

获取网络Ip地址

欢马劈雪     最近更新时间:2020-01-02 10:19:05

519
获取网络Ip地址


//package com.nowjava;

import java.io.BufferedReader;

import java.io.IOException;//时代Java - N o w  J a v a . c o m 提供

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;


import java.net.MalformedURLException;


import java.net.URL;

import java.net.URLConnection;


public class Main {

    public static void main(String[] argv) throws Exception {/**from 时代Java公众号 - N o w J a  v a . c o m**/

        System.out.println(GetNetIp());

    }


    public static String GetNetIp() {

        URL infoUrl = null;

        InputStream inStream = null;

        try {

            infoUrl = new URL("http://iframe.ip138.com/ic.asp");

            URLConnection connection = infoUrl.openConnection();

            HttpURLConnection httpConnection = (HttpURLConnection) connection;

            int responseCode = httpConnection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {

                inStream = httpConnection.getInputStream();

                BufferedReader reader = new BufferedReader(

                        new InputStreamReader(inStream, "utf-8"));

                StringBuilder strber = new StringBuilder();

                String line = null;

                while ((line = reader.readLine()) != null) {

                    strber.append(line + "\n");

                }

                inStream.close();

                int start = strber.indexOf("[");

                int end = strber.indexOf(
展开阅读全文