Skip to content

Commit 6ec77f4

Browse files
Added support for IP2Location Web Service
1 parent 35c66d0 commit 6ec77f4

File tree

6 files changed

+434
-4
lines changed

6 files changed

+434
-4
lines changed

README.md

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ This component allows user to query an IP address for info such as the visitor
55
* Free IP2Location BIN Data: https://lite.ip2location.com
66
* Commercial IP2Location BIN Data: https://www.ip2location.com/database/ip2location
77

8+
As an alternative, this component can also call the IP2Location Web Service. This requires an API key. If you don't have an existing API key, you can subscribe for one at the below:
9+
10+
https://www.ip2location.com/web-service/ip2location
11+
812
## Compilation
913

1014
```bash
1115
javac com/ip2location/*.java
1216
jar cf ip2location.jar com/ip2location/*.class
1317
```
1418

19+
## QUERY USING THE BIN FILE
20+
1521
## Parameters
1622
Below are the parameters to set before using this class.
1723

@@ -129,3 +135,196 @@ public class Main
129135
}
130136
```
131137

138+
## QUERY USING THE WEB SERVICE
139+
140+
## Methods
141+
Below are the methods supported in this class.
142+
143+
|Method Name|Description|
144+
|---|---|
145+
|Open(String APIKey, String Package, boolean UseSSL)|Initialize component.|
146+
|IPQuery(String IPAddress)|Query IP address. This method returns a JsonObject.|
147+
|IPQuery(String IPAddress, String Language)|Query IP address and translation language. This method returns a JsonObject.|
148+
|IPQuery(String IPAddress, String[] AddOns, String Language)|Query IP address and Addons. This method returns a JsonObject.|
149+
|GetCredit()|This method returns the web service credit balance in a JsonObject.|
150+
151+
Below are the Addons supported in this class.
152+
153+
|Addon Name|Description|
154+
|---|---|
155+
|continent|Returns continent code, name, hemispheres and translations.|
156+
|country|Returns country codes, country name, flag, capital, total area, population, currency info, language info, IDD, TLD and translations.|
157+
|region|Returns region code, name and translations.|
158+
|city|Returns city name and translations.|
159+
|geotargeting|Returns metro code based on the ZIP/postal code.|
160+
|country_groupings|Returns group acronyms and names.|
161+
|time_zone_info|Returns time zones, DST, GMT offset, sunrise and sunset.|
162+
163+
## Result fields
164+
Below are the result fields.
165+
166+
|Name|
167+
|---|
168+
|<ul><li>country_code</li><li>country_name</li><li>region_name</li><li>city_name</li><li>latitude</li><li>longitude</li><li>zip_code</li><li>time_zone</li><li>isp</li><li>domain</li><li>net_speed</li><li>idd_code</li><li>area_code</li><li>weather_station_code</li><li>weather_station_name</li><li>mcc</li><li>mnc</li><li>mobile_brand</li><li>elevation</li><li>usage_type</li><li>continent<ul><li>name</li><li>code</li><li>hemisphere</li><li>translations</li></ul></li><li>country<ul><li>name</li><li>alpha3_code</li><li>numeric_code</li><li>demonym</li><li>flag</li><li>capital</li><li>total_area</li><li>population</li><li>currency<ul><li>code</li><li>name</li><li>symbol</li></ul></li><li>language<ul><li>code</li><li>name</li></ul></li><li>idd_code</li><li>tld</li><li>translations</li></ul></li><li>region<ul><li>name</li><li>code</li><li>translations</li></ul></li><li>city<ul><li>name</li><li>translations</li></ul></li><li>geotargeting<ul><li>metro</li></ul></li><li>country_groupings</li><li>time_zone_info<ul><li>olson</li><li>current_time</li><li>gmt_offset</li><li>is_dst</li><li>sunrise</li><li>sunset</li></ul></li><ul>|
169+
170+
## Usage
171+
172+
```java
173+
import com.ip2location.*;
174+
import com.google.gson.*;
175+
176+
public class Main
177+
{
178+
public Main()
179+
{
180+
}
181+
public static void main(String[] args)
182+
{
183+
try
184+
{
185+
IP2LocationWebService ws = new IP2LocationWebService();
186+
187+
String strIPAddress = "8.8.8.8";
188+
String strAPIKey = "YOUR_API_KEY_HERE";
189+
String strPackage = "WS24";
190+
String[] addOn = {"continent", "country", "region", "city", "geotargeting", "country_groupings", "time_zone_info"};
191+
String strLang = "es";
192+
boolean boolSSL = true;
193+
194+
ws.Open(strAPIKey, strPackage, boolSSL);
195+
196+
JsonObject myresult = ws.IPQuery(strIPAddress, addOn, strLang);
197+
198+
if (myresult.get("response") == null)
199+
{
200+
// standard results
201+
System.out.println("country_code: " + ((myresult.get("country_code") != null) ? myresult.get("country_code").getAsString() : ""));
202+
System.out.println("country_name: " + ((myresult.get("country_name") != null) ? myresult.get("country_name").getAsString() : ""));
203+
System.out.println("region_name: " + ((myresult.get("region_name") != null) ? myresult.get("region_name").getAsString() : ""));
204+
System.out.println("city_name: " + ((myresult.get("city_name") != null) ? myresult.get("city_name").getAsString() : ""));
205+
System.out.println("latitude: " + ((myresult.get("latitude") != null) ? myresult.get("latitude").getAsString() : ""));
206+
System.out.println("longitude: " + ((myresult.get("longitude") != null) ? myresult.get("longitude").getAsString() : ""));
207+
System.out.println("zip_code: " + ((myresult.get("zip_code") != null) ? myresult.get("zip_code").getAsString() : ""));
208+
System.out.println("time_zone: " + ((myresult.get("time_zone") != null) ? myresult.get("time_zone").getAsString() : ""));
209+
System.out.println("isp: " + ((myresult.get("isp") != null) ? myresult.get("isp").getAsString() : ""));
210+
System.out.println("domain: " + ((myresult.get("domain") != null) ? myresult.get("domain").getAsString() : ""));
211+
System.out.println("net_speed: " + ((myresult.get("net_speed") != null) ? myresult.get("net_speed").getAsString() : ""));
212+
System.out.println("idd_code: " + ((myresult.get("idd_code") != null) ? myresult.get("idd_code").getAsString() : ""));
213+
System.out.println("area_code: " + ((myresult.get("area_code") != null) ? myresult.get("area_code").getAsString() : ""));
214+
System.out.println("weather_station_code: " + ((myresult.get("weather_station_code") != null) ? myresult.get("weather_station_code").getAsString() : ""));
215+
System.out.println("weather_station_name: " + ((myresult.get("weather_station_name") != null) ? myresult.get("weather_station_name").getAsString() : ""));
216+
System.out.println("mcc: " + ((myresult.get("mcc") != null) ? myresult.get("mcc").getAsString() : ""));
217+
System.out.println("mnc: " + ((myresult.get("mnc") != null) ? myresult.get("mnc").getAsString() : ""));
218+
System.out.println("mobile_brand: " + ((myresult.get("mobile_brand") != null) ? myresult.get("mobile_brand").getAsString() : ""));
219+
System.out.println("elevation: " + ((myresult.get("elevation") != null) ? myresult.get("elevation").getAsString() : ""));
220+
System.out.println("usage_type: " + ((myresult.get("usage_type") != null) ? myresult.get("usage_type").getAsString() : ""));
221+
System.out.println("credits_consumed: " + ((myresult.get("credits_consumed") != null) ? myresult.get("credits_consumed").getAsString() : ""));
222+
223+
// continent addon
224+
if (myresult.get("continent") != null)
225+
{
226+
JsonObject continentObj = myresult.getAsJsonObject("continent");
227+
System.out.println("continent => name: " + continentObj.get("name").getAsString());
228+
System.out.println("continent => code: " + continentObj.get("code").getAsString());
229+
JsonArray myarr = continentObj.getAsJsonArray("hemisphere");
230+
System.out.println("continent => hemisphere: " + myarr.toString());
231+
System.out.println("continent => translations: " + continentObj.getAsJsonObject("translations").get(strLang).getAsString());
232+
}
233+
234+
// country addon
235+
if (myresult.get("country") != null)
236+
{
237+
JsonObject countryObj = myresult.getAsJsonObject("country");
238+
System.out.println("country => name: " + countryObj.get("name").getAsString());
239+
System.out.println("country => alpha3_code: " + countryObj.get("alpha3_code").getAsString());
240+
System.out.println("country => numeric_code: " + countryObj.get("numeric_code").getAsString());
241+
System.out.println("country => demonym: " + countryObj.get("demonym").getAsString());
242+
System.out.println("country => flag: " + countryObj.get("flag").getAsString());
243+
System.out.println("country => capital: " + countryObj.get("capital").getAsString());
244+
System.out.println("country => total_area: " + countryObj.get("total_area").getAsString());
245+
System.out.println("country => population: " + countryObj.get("population").getAsString());
246+
System.out.println("country => idd_code: " + countryObj.get("idd_code").getAsString());
247+
System.out.println("country => tld: " + countryObj.get("tld").getAsString());
248+
System.out.println("country => translations: " + countryObj.getAsJsonObject("translations").get(strLang).getAsString());
249+
250+
JsonObject currencyObj = countryObj.getAsJsonObject("currency");
251+
System.out.println("country => currency => code: " + currencyObj.get("code").getAsString());
252+
System.out.println("country => currency => name: " + currencyObj.get("name").getAsString());
253+
System.out.println("country => currency => symbol: " + currencyObj.get("symbol").getAsString());
254+
255+
JsonObject languageObj = countryObj.getAsJsonObject("language");
256+
System.out.println("country => language => code: " + languageObj.get("code").getAsString());
257+
System.out.println("country => language => name: " + languageObj.get("name").getAsString());
258+
}
259+
260+
// region addon
261+
if (myresult.get("region") != null)
262+
{
263+
JsonObject regionObj = myresult.getAsJsonObject("region");
264+
System.out.println("region => name: " + regionObj.get("name").getAsString());
265+
System.out.println("region => code: " + regionObj.get("code").getAsString());
266+
System.out.println("region => translations: " + regionObj.getAsJsonObject("translations").get(strLang).getAsString());
267+
}
268+
269+
// city addon
270+
if (myresult.get("city") != null)
271+
{
272+
JsonObject cityObj = myresult.getAsJsonObject("city");
273+
System.out.println("city => name: " + cityObj.get("name").getAsString());
274+
System.out.println("city => translations: " + cityObj.getAsJsonArray("translations").toString());
275+
}
276+
277+
// geotargeting addon
278+
if (myresult.get("geotargeting") != null)
279+
{
280+
JsonObject geoObj = myresult.getAsJsonObject("geotargeting");
281+
System.out.println("geotargeting => metro: " + geoObj.get("metro").getAsString());
282+
}
283+
284+
// country_groupings addon
285+
if (myresult.get("country_groupings") != null)
286+
{
287+
JsonArray myarr = myresult.getAsJsonArray("country_groupings");
288+
if (myarr.size() > 0)
289+
{
290+
for (int x = 0; x < myarr.size(); x++)
291+
{
292+
System.out.println("country_groupings => #" + x + " => acronym: " + myarr.get(x).getAsJsonObject().get("acronym").getAsString());
293+
System.out.println("country_groupings => #" + x + " => name: " + myarr.get(x).getAsJsonObject().get("name").getAsString());
294+
}
295+
}
296+
}
297+
298+
// time_zone_info addon
299+
if (myresult.get("time_zone_info") != null)
300+
{
301+
JsonObject tzObj = myresult.getAsJsonObject("time_zone_info");
302+
System.out.println("time_zone_info => olson: " + tzObj.get("olson").getAsString());
303+
System.out.println("time_zone_info => current_time: " + tzObj.get("current_time").getAsString());
304+
System.out.println("time_zone_info => gmt_offset: " + tzObj.get("gmt_offset").getAsString());
305+
System.out.println("time_zone_info => is_dst: " + tzObj.get("is_dst").getAsString());
306+
System.out.println("time_zone_info => sunrise: " + tzObj.get("sunrise").getAsString());
307+
System.out.println("time_zone_info => sunset: " + tzObj.get("sunset").getAsString());
308+
}
309+
}
310+
else
311+
{
312+
System.out.println("Error: " + myresult.get("response").getAsString());
313+
}
314+
315+
myresult = ws.GetCredit();
316+
317+
if (myresult.get("response") != null)
318+
{
319+
System.out.println("Credit balance: " + myresult.get("response").getAsString());
320+
}
321+
}
322+
catch(Exception e)
323+
{
324+
System.out.println(e);
325+
e.printStackTrace(System.out);
326+
}
327+
}
328+
}
329+
330+
```

com/ip2location/Http.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.ip2location;
2+
3+
import java.io.BufferedReader;
4+
import java.io.DataOutputStream;
5+
import java.io.InputStreamReader;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
9+
class Http {
10+
public static String get(URL url) {
11+
try {
12+
java.lang.System.setProperty("https.protocols", "TLSv1.2");
13+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
14+
conn.setRequestMethod("GET");
15+
conn.setRequestProperty("Accept", "application/json");
16+
17+
if (conn.getResponseCode() != 200) {
18+
return ("Failed : HTTP error code : " + conn.getResponseCode());
19+
}
20+
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
21+
22+
String output;
23+
StringBuilder resultFromHttp = new StringBuilder();
24+
while ((output = br.readLine()) != null) {
25+
resultFromHttp.append(output).append("\n");
26+
}
27+
28+
br.close();
29+
conn.disconnect();
30+
return resultFromHttp.toString();
31+
} catch (Exception e) {
32+
throw new RuntimeException(e);
33+
}
34+
}
35+
36+
public static String post(URL url, String post) {
37+
try {
38+
java.lang.System.setProperty("https.protocols", "TLSv1.2");
39+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
40+
conn.setRequestMethod("POST");
41+
conn.setRequestProperty("Accept", "application/json");
42+
43+
String urlParameters = post;
44+
45+
conn.setDoOutput(true);
46+
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
47+
dos.writeBytes(urlParameters);
48+
dos.flush();
49+
dos.close();
50+
51+
if (conn.getResponseCode() != 200) {
52+
return ("Failed : HTTP error code : " + conn.getResponseCode());
53+
}
54+
55+
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
56+
57+
String output;
58+
StringBuilder resultFromHttp = new StringBuilder();
59+
while ((output = br.readLine()) != null) {
60+
resultFromHttp.append(output).append("\n");
61+
}
62+
63+
br.close();
64+
conn.disconnect();
65+
return resultFromHttp.toString();
66+
} catch (Exception e) {
67+
throw new RuntimeException(e);
68+
}
69+
}
70+
}

com/ip2location/IP2Location.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* <p>
3737
*
3838
* @author IP2Location.com
39-
* @version 8.4.0
39+
* @version 8.5.0
4040
*/
4141
public class IP2Location {
4242
private static final Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); // IPv4

0 commit comments

Comments
 (0)