[Java]
/**
* Obtain a non-lan ip Address
*
* @ Return
* @ Author SHANHY
*/
Public static String getPsdnIp (){
String ipurl = "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;
}
Author: hehe9737