Java Access to public network (extranet IP) is very practical!

Source: Internet
Author: User
Tags stringbuffer
Package com.lovo.util;
Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.net.HttpURLConnection;

Import Java.net.URL; public class Publicinterip {/** * @param args * @throws Exception/public String Getpublicip () {try {Str  ing path = "http://iframe.ip138.com/ic.asp";//To get the address URL URL of HTML page content = new URL (path);//Create URL object httpurlconnection conn = (httpurlconnection) url.openconnection ();//Open Connection Conn.setrequestproperty ("ContentType", "GBK"); Set URL Chinese parameter encoding conn.setconnecttimeout (5 * 1000);//Requested Time Conn.setrequestmethod ("get");//Request Way InputStream Inst
			Ream = Conn.getinputstream ();

			Readlesosysxml (instream);
			BufferedReader in = new BufferedReader (new InputStreamReader (instream, "GBK"));
			StringBuffer buffer = new StringBuffer ();
			String line = "";
			Reads the last line fetched to the content, writes while (line = In.readline ())!= null) {buffer.append (line);
			String str = buffer.tostring ();String ipString1 = str.substring (Str.indexof ("[")); Get your IP is in the middle of [182.149.82.50] content String ipsString2 = ipstring1.substring (Ipstring1.indexof ("[") + 1, Ipstring1.lasti
			Ndexof ("]")); Gets the address of the current IP address/* String ipsstring3=ipstring1.substring (Ipstring1.indexof (":"), Ipstring1.lastindexof ("</center
			> "));

		System.err.println (IPSSTRING3);//Back to public IP value return ipsString2;
			catch (Exception e) {System.out.println ("Get public network IP Connection timeout");
		Return "Connection Timeout";
		} public static void Main (string[] args) {Publicinterip interip=new publicinterip ();
	System.err.println (Interip.getpublicip ()); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.