Skip to content

Commit 02abb22

Browse files
committed
Add examples with API key. Divide examples into 2 groups
- Divide examples into two groups: access by passwrod and by API key - Add java code sample with API key access example
1 parent 71bb622 commit 02abb22

33 files changed

Lines changed: 187 additions & 0 deletions

apikey/java/.classpath

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" path="src/main/resources/config"/>
10+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
11+
<attributes>
12+
<attribute name="optional" value="true"/>
13+
<attribute name="maven.pomderived" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
27+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
28+
<classpathentry kind="output" path="target/classes"/>
29+
</classpath>

apikey/java/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>TestClient</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

apikey/java/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>LLC</groupId>
5+
<artifactId>com.whoisxmlapi</artifactId>
6+
<version>1.0</version>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>commons-httpclient</groupId>
11+
<artifactId>commons-httpclient</artifactId>
12+
<version>3.1</version>
13+
</dependency>
14+
<dependency>
15+
<groupId>org.json</groupId>
16+
<artifactId>json</artifactId>
17+
<version>20160810</version>
18+
</dependency>
19+
20+
</dependencies>
21+
</project>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.whoisxmlapi.test;
2+
3+
import java.net.URLEncoder;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
6+
7+
import javax.crypto.Mac;
8+
import javax.crypto.spec.SecretKeySpec;
9+
10+
import org.apache.commons.codec.binary.Base64;
11+
import org.apache.commons.codec.binary.Hex;
12+
import org.apache.commons.httpclient.HttpClient;
13+
import org.apache.commons.httpclient.HttpMethod;
14+
import org.apache.commons.httpclient.methods.GetMethod;
15+
import org.json.JSONException;
16+
import org.json.JSONObject;
17+
18+
public class ApiKeyClientTest {
19+
20+
private Logger logger = Logger.getLogger(ApiKeyClientTest.class.getName());
21+
22+
public static void main(String[]args) {
23+
new ApiKeyClientTest().getSimpleDomainUsingApiKey();
24+
}
25+
26+
private void getSimpleDomainUsingApiKey() {
27+
String domainName = "test.com";
28+
29+
String username = "username";
30+
String apiKey = "apiKey";
31+
String secretKey = "secretKey";
32+
33+
getDomainNameUsingApiKey(domainName, username, apiKey, secretKey);
34+
}
35+
36+
private String executeURL(String url) {
37+
HttpClient c = new HttpClient();
38+
System.out.println(url);
39+
HttpMethod m = new GetMethod(url);
40+
String res = null;
41+
try {
42+
c.executeMethod(m);
43+
res = new String(m.getResponseBody());
44+
} catch (Exception e) {
45+
logger.log(Level.SEVERE, "Cannot get url", e);
46+
} finally {
47+
m.releaseConnection();
48+
}
49+
return res;
50+
}
51+
52+
public void getDomainNameUsingApiKey(String domainName, String username, String apiKey, String secretKey) {
53+
String apiKeyAuthenticationRequest = generateApiKeyAuthenticationRequest(username, apiKey, secretKey);
54+
if (apiKeyAuthenticationRequest == null) {
55+
return;
56+
}
57+
58+
StringBuilder sb = new StringBuilder();
59+
sb.append("http://www.whoisxmlapi.com/whoisserver/WhoisService?");
60+
sb.append(apiKeyAuthenticationRequest);
61+
sb.append("&domainName=");
62+
sb.append(domainName);
63+
64+
String url = sb.toString();
65+
66+
String result = executeURL(url);
67+
if (result != null) {
68+
logger.log(Level.INFO, "result: " + result);
69+
}
70+
}
71+
72+
private String generateApiKeyAuthenticationRequest(String username, String apiKey, String secretKey) {
73+
try {
74+
long timestamp = System.currentTimeMillis();
75+
76+
String request = generateRequest(username, timestamp);
77+
String digest = generateDigest(username, apiKey, secretKey, timestamp);
78+
79+
String requestURL = URLEncoder.encode(request, "UTF-8");
80+
String digestURL = URLEncoder.encode(digest, "UTF-8");
81+
82+
String apiKeyAuthenticationRequest = "requestObject="+requestURL+"&digest="+digestURL;
83+
return apiKeyAuthenticationRequest;
84+
} catch (Exception e) {
85+
logger.log(Level.SEVERE, "an error occurred", e);
86+
}
87+
return null;
88+
}
89+
90+
private String generateRequest(String username, long timestamp) throws JSONException {
91+
JSONObject json = new JSONObject();
92+
json.put("u", username);
93+
json.put("t", timestamp);
94+
String jsonStr = json.toString();
95+
byte[] json64 = Base64.encodeBase64(jsonStr.getBytes());
96+
String json64Str = new String(json64);
97+
return json64Str;
98+
}
99+
100+
private String generateDigest(String username, String apiKey, String secretKey, long timestamp) throws Exception {
101+
StringBuilder sb = new StringBuilder();
102+
sb.append(username);
103+
sb.append(timestamp);
104+
sb.append(apiKey);
105+
106+
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacMD5");
107+
Mac mac = Mac.getInstance(secretKeySpec.getAlgorithm());
108+
mac.init(secretKeySpec);
109+
110+
byte[] digestBytes = mac.doFinal(sb.toString().getBytes("UTF-8"));
111+
String digest = new String(Hex.encodeHex(digestBytes));
112+
return digest;
113+
}
114+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)