Make sure the "responseCode" is "200-OK", before trying to get the InputStream in "UriBuilder.getPublicIP()".
This commit is contained in:
parent
fd62ac567e
commit
d96d0c68cd
|
@ -56,23 +56,30 @@ public class UriBuilder {
|
||||||
{
|
{
|
||||||
String publicIpAddress = "";
|
String publicIpAddress = "";
|
||||||
HttpURLConnection conn = null;
|
HttpURLConnection conn = null;
|
||||||
|
String urlString = "https://api.ipify.org/";
|
||||||
try {
|
try {
|
||||||
conn = (HttpURLConnection) new URL("https://api.ipify.org/").openConnection();
|
conn = (HttpURLConnection) new URL(urlString).openConnection();
|
||||||
conn.setConnectTimeout(60_000);
|
conn.setConnectTimeout(60_000);
|
||||||
conn.setReadTimeout(60_000);
|
conn.setReadTimeout(60_000);
|
||||||
conn.setRequestMethod("GET");
|
conn.setRequestMethod("GET");
|
||||||
conn.connect();
|
conn.connect();
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
if ( responseCode != 200 ) {
|
||||||
|
logger.warn("Cannot get the publicIP address for this machine, as \"" + urlString + "\" returned the HTTP-error-code: " + responseCode);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try ( BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
|
try ( BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
|
||||||
publicIpAddress = bf.readLine().trim();
|
publicIpAddress = bf.readLine().trim();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.warn("Cannot get the publicIP address for this machine!", e);
|
logger.warn("Cannot get the publicIP address for this machine, from \"" + urlString + "\"!", e);
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
if ( conn != null )
|
if ( conn != null )
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
return publicIpAddress;
|
return publicIpAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue