Java obtains the client logon address (country, province, city, etc.), java Province

Source: Internet
Author: User

Java obtains the client logon address (country, province, city, etc.), java Province

Original article: java obtains the client logon address (country, province, city, etc)

Source code: http://www.zuidaima.com/share/1550463687658496.htm

Java obtains the client logon address (country, province, city, etc)

/*** @ Param urlStr * request address * @ param content * @ author www.zuidaima.com * The request parameter format is: name = xxx & pwd = xxx * @ param encoding * server-side Request encoding. Such as GBK, UTF-8, etc. * @ return */private static String getResult (String urlStr, String content, String encoding) {URL url = null; HttpURLConnection connection = null; try {url = new URL (urlStr); connection = (HttpURLConnection) url. openConnection (); // create a connection to the instance. setConnectTimeout (2000); // sets the connection timeout time, in milliseconds. setReadTimeout (2000); // set the timeout value for Data Reading, in milliseconds. setDoOutput (true); // whether to enable the output stream. true | False connection. setDoInput (true); // whether to enable the input stream true | false connection. setRequestMethod ("POST"); // submit method POST | GET connection. setUseCaches (false); // whether to cache true | false connection. connect (); // open the connection port DataOutputStream out = new DataOutputStream (connection. getOutputStream (); // open the output stream and write data out to the peer server. writeBytes (content); // write data, that is, submit your form name = xxx & pwd = xxx out. flush (); // refresh out. close (); // close the output stream BufferedReader reader = New BufferedReader (new InputStreamReader (connection. getInputStream (), encoding); // writes data to the peer end. The peer server returns data and reads the StringBuffer buffer = new StringBuffer (); String line = ""; while (line = reader. readLine ())! = Null) {buffer. append (line);} reader. close (); return buffer. toString ();} catch (IOException e) {e. printStackTrace ();} finally {if (connection! = Null) {connection. disconnect (); // close the connection} return null;}/*** @ Title: decodeUnicode * @ author kaka * @ Description: unicode conversion to Chinese * @ param theString * @ param @ return * @ return String * @ throws */private static String decodeUnicode (String theString) {char aChar; int len = theString. length (); StringBuffer outBuffer = new StringBuffer (len); for (int x = 0; x <len;) {aChar = theString. cha RAt (x ++); if (aChar = '\') {aChar = theString. charAt (x ++); if (aChar = 'U') {int value = 0; for (int I = 0; I <4; I ++) {aChar = theString. charAt (x ++); switch (aChar) {case '0': case '1': case '2': case '3': case '4 ': case '5': case '6': case '7': case '8': case '9': value = (value <4) + aChar-'0 '; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value <4) + 10 + aChar-'A'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F ': value = (value <4) + 10 + aChar-'A'; break; default: throw new IllegalArgumentException ("Malformed encoding. ") ;}} outBuffer. append (char) value);} else {if (aChar = 'T') {aChar = '\ T';} else if (aChar = 'R ') {aChar = '\ R';} else if (aChar = 'n') {aChar =' \ n';} else if (aChar = 'F ') {aChar = '\ F';} outBu Ffer. append (aChar) ;}} else {outBuffer. append (aChar) ;}} return outBuffer. toString ();}/***** @ param content * request parameter format: name = xxx & pwd = xxx * @ param encoding * server-side Request encoding. Such as GBK, UTF-8, etc. * @ return * @ throws UnsupportedEncodingException */public static String getAddresses (String content, String encodingString) throws UnsupportedEncodingException {String result = null; stringBuffer sb = new StringBuffer (); // here call the pconline interface String urlStr = "http://ip.taobao.com/service/getIpInfo.php"; // get the information of the province and city where the IP is located from the http://whois.pconline.com.cn String returnStr = getResult (urlStr, con Tent, encodingString); if (returnStr! = Null) {// process the returned Provincial/Municipal Information String [] temp = returnStr. split (","); if (temp. length <3) {return "0"; // invalid IP address, lan test} String country = ""; String area = ""; String region = ""; string city = ""; String county = ""; String isp = ""; Boolean flag = false; for (int I = 0; I <temp. length; I ++) {if (flag) {return result;} switch (I) {case 1: country = (temp [I]. split (":") [2]. replaceAll ("\" "," "); country = decodeUnicode (country); // country if (country. equals ("unallocated or Intranet IP Address") {flag = true; sb. append (country); result = sb. toString (); break;} sb. append ("Country:"); sb. append (country); sb. append (","); break; case 3: area = (temp [I]. split (":") [1]. replaceAll ("\" "," "); area = decodeUnicode (area); // region break; case 5: region = (temp [I]. split (":") [1]. replaceAll ("\" "," "); region = decodeUnicode (region); // province sb. append ("province:"); sb. append (region); sb. append (","); break; case 7: city = (temp [I]. split (":") [1]. replaceAll ("\" "," "); city = decodeUnicode (city); // urban sb. append ("City:"); sb. append (city); sb. append (","); break; case 9: county = (temp [I]. split (":") [1]. replaceAll ("\" "," "); county = decodeUnicode (county); // region sb. append ("region:"); sb. append (county); break; case 11: isp = (temp [I]. split (":") [1]. replaceAll ("\" "," "); isp = decodeUnicode (isp); // ISP company break ;}} result = sb. toString ();} return result ;}

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.