To determine the proxy type of an IP address using IP2Location.io in Scala, you can use the IP2Location.io API to retrieve geolocation and related data. For proxy types supported, please see API documentation for more info.
import com.google.gson.*
import java.net.URI
import java.net.http.{HttpClient, HttpRequest, HttpResponse}
@main def Test(): Unit = {
try {
val key = "YOUR_API_KEY"
val ip = "8.8.8.8"
val url = "https://api.ip2location.io/?format=json&key=" + key + "&ip=" + ip
val request = HttpRequest.newBuilder.uri(new URI(url)).GET.build
val client = HttpClient.newHttpClient
val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString).join()
val statusCode = response.statusCode()
if (statusCode == 200) {
val rawJSON = response.body()
val myObj = JsonParser.parseString(rawJSON).getAsJsonObject
if (myObj.has("proxy")) {
System.out.println("The proxy type for IP " + ip + " is " + myObj.getAsJsonObject("proxy").get("proxy_type").getAsString + ".")
} else {
throw Exception("ERROR: The proxy_type field requires a paid subscription to the Security plan.")
}
} else if (statusCode == 400 || statusCode == 401) {
val rawJSON = response.body()
if (rawJSON.contains("error_message")) {
throw Exception("ERROR: " + JsonParser.parseString(rawJSON).getAsJsonObject.getAsJsonObject("error")
.get("error_message").getAsString
)
}
throw Exception(rawJSON)
} else {
throw Exception(response.body())
}
} catch {
case e: Exception => System.out.println(e)
}
}
This script will output the proxy type of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free