欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

获取非局域网的ip地址

程序员文章站 2022-04-14 10:16:06
[java]  /**      * 获取非局域网的ip地址      *   &nbs...
[java] 
/**
     * 获取非局域网的ip地址
     *
     * @return
     * @author shanhy
     */ 
    public static string getpsdnip() { 
        string ipurl = "jsp">www.2cto.com"; 
        string ss =null; 
        httpurlconnection urlconnection = null; 
        try { 
            urlconnection = null; 
            try { 
                url url = new url(ipurl); 
                urlconnection = (httpurlconnection) url.openconnection(); 
                urlconnection.setdoinput(true); 
                urlconnection 
                        .setconnecttimeout(woconfiguration.time_out_length); 
                urlconnection.setreadtimeout(woconfiguration.time_out_length); 
 
                urlconnection.setrequestproperty("content-type", 
                        "application/x-www-form-urlencoded"); 
 
                urlconnection.connect(); 
 
                int response = urlconnection.getresponsecode(); 
                 
                if (response == httpconnection.http_ok) { 
                    inputstream in = urlconnection.getinputstream();// .openinputstream(); 
                    bytearrayoutputstream baos = new bytearrayoutputstream(); 
                    byte[] buff = new byte[1024]; 
                    int rc = 0; 
                    while ((rc = in.read(buff, 0, 1024)) != -1) { 
                        baos.write(buff, 0, rc); 
                    } 
                    byte[] content = baos.tobytearray(); 
                    ss = new string(content); 
                    in.close(); 
                    in = null; 
                    baos.close(); 
                    baos = null; 
                    jsonobject sn=new jsonobject(ss.substring(ss.indexof("({")+1,ss.indexof("})")+1)); 
                    ss=sn.getstring("ip"); 
                } else { 
                } 
            } catch (exception e) { 
                e.printstacktrace(); 
            } 
        } catch (exception e) { 
            return null; 
        } finally { 
            if (urlconnection != null) { 
                urlconnection.disconnect();// .close(); 
                urlconnection = null; 
            } 
        } 
        return ss; 
    } 
作者:hehe9737