Skip to content

Commit 294b06a

Browse files
committed
.Net C# code samples for API key auth
1 parent b770279 commit 294b06a

4 files changed

Lines changed: 460 additions & 0 deletions

File tree

apikey/net/ApiKeyAuthTest.cs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Security.Cryptography;
6+
using System.Net;
7+
using System.IO;
8+
using Newtonsoft.Json;
9+
10+
/*
11+
* Target platform: .Net Framework 4.0
12+
*
13+
* You need to install Newtonsoft JSON.NET
14+
*
15+
*/
16+
17+
18+
namespace ApiKeyAuthTest
19+
{
20+
class ApiKeyAuth
21+
{
22+
static void Main(string[] args)
23+
{
24+
new ApiKeyAuth().Run();
25+
}
26+
27+
public void Run()
28+
{
29+
string username = "username";
30+
string apiKey = "api_key";
31+
string secretKey = "secret_key";
32+
string url = "https://whoisxmlapi.com/whoisserver/WhoisService?";
33+
string[] domains =
34+
{
35+
"google.com",
36+
"example.com",
37+
"whoisxmlapi.com",
38+
"twitter.com"
39+
};
40+
bool reuseDigest = true;
41+
42+
this.PerformRequest(username, apiKey, secretKey,
43+
url, domains, reuseDigest);
44+
}
45+
46+
protected void PerformRequest(string username, string apiKey,
47+
string secretKey, string url, string[] domains, bool reuseDigest)
48+
{
49+
long timestamp = this.GetTimeStamp();
50+
string digest = this.GenerateDigest(
51+
username, apiKey, secretKey, timestamp
52+
);
53+
54+
foreach (string domain in domains)
55+
{
56+
try
57+
{
58+
if (!reuseDigest)
59+
{
60+
timestamp = this.GetTimeStamp();
61+
digest = this.GenerateDigest(
62+
username, apiKey, secretKey, timestamp
63+
);
64+
}
65+
string request = this.BuildRequest(
66+
username, timestamp, digest, domain
67+
);
68+
string response = this.GetWhoisData(url + request);
69+
if (response.IndexOf("Request timeout") > -1)
70+
{
71+
timestamp = this.GetTimeStamp();
72+
digest = this.GenerateDigest(
73+
username, apiKey, secretKey, timestamp
74+
);
75+
request = this.BuildRequest(
76+
username, timestamp, digest, domain
77+
);
78+
response = this.GetWhoisData(url + request);
79+
}
80+
this.PrintResponse(response);
81+
} catch(Exception e)
82+
{
83+
Console.WriteLine(
84+
"Error occurred\r\nCannot get whois data for "
85+
+ domain
86+
);
87+
}
88+
}
89+
Console.ReadLine();
90+
}
91+
92+
protected string GenerateDigest(
93+
string username, string apiKey, string secretKey, long timestamp
94+
)
95+
{
96+
string data = username + timestamp + apiKey;
97+
HMACMD5 hmac = new HMACMD5(Encoding.UTF8.GetBytes(secretKey));
98+
string hex = BitConverter.ToString(
99+
hmac.ComputeHash(Encoding.UTF8.GetBytes(data))
100+
);
101+
return hex.Replace("-", "").ToLower();
102+
}
103+
104+
protected string BuildRequest(
105+
string username, long timestamp, string digest, string domain
106+
)
107+
{
108+
UserData ud = new UserData();
109+
ud.u = username;
110+
ud.t = timestamp;
111+
string userData = JsonConvert.SerializeObject(ud, Formatting.None);
112+
var userDataBytes = System.Text.Encoding.UTF8.GetBytes(userData);
113+
string userDataBase64 = System.Convert.ToBase64String(userDataBytes);
114+
115+
StringBuilder requestString = new StringBuilder();
116+
requestString.Append("requestObject=");
117+
requestString.Append(Uri.EscapeDataString(userDataBase64));
118+
requestString.Append("&digest=");
119+
requestString.Append(Uri.EscapeDataString(digest));
120+
requestString.Append("&domainName=");
121+
requestString.Append(domain);
122+
requestString.Append("&outputFormat=json");
123+
124+
return requestString.ToString();
125+
}
126+
127+
protected string GetWhoisData(string url)
128+
{
129+
string response;
130+
try {
131+
WebRequest wr = WebRequest.Create(url);
132+
WebResponse wp = wr.GetResponse();
133+
134+
using (Stream data = wp.GetResponseStream())
135+
{
136+
using (StreamReader reader = new StreamReader(data))
137+
{
138+
response = reader.ReadToEnd();
139+
}
140+
}
141+
wp.Close();
142+
} catch (Exception e)
143+
{
144+
Console.WriteLine(e.Message);
145+
throw e;
146+
}
147+
return response;
148+
}
149+
150+
protected void PrintResponse(string response)
151+
{
152+
dynamic responseObject = JsonConvert.DeserializeObject(response);
153+
154+
if (responseObject.WhoisRecord != null)
155+
{
156+
var whois = responseObject.WhoisRecord;
157+
if (whois.createdDate != null)
158+
{
159+
Console.WriteLine(
160+
"Created date: "
161+
+ whois.createdDate.ToString()
162+
);
163+
}
164+
if (whois.expiresDate != null)
165+
{
166+
Console.WriteLine(
167+
"Expires date: "
168+
+ whois.expiresDate.ToString()
169+
);
170+
}
171+
if (whois.domainName != null)
172+
{
173+
Console.WriteLine(
174+
"Domain name: "
175+
+ whois.domainName.ToString()
176+
);
177+
}
178+
Console.WriteLine("--------------------------------");
179+
return;
180+
}
181+
182+
Console.WriteLine(response);
183+
}
184+
185+
protected long GetTimeStamp()
186+
{
187+
return (long)(DateTime.Now.Subtract(
188+
new DateTime(1970, 1, 1)
189+
).TotalMilliseconds * 1000);
190+
}
191+
}
192+
193+
class UserData
194+
{
195+
public string u { get; set; }
196+
public long t { get; set; }
197+
}
198+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Security.Cryptography;
6+
using System.Net;
7+
using System.IO;
8+
using Newtonsoft.Json;
9+
10+
/*
11+
* Target platform: .Net Framework 4.0
12+
*
13+
* You need to install Newtonsoft JSON.NET
14+
*
15+
*/
16+
17+
18+
namespace ApiKeyAuthTest
19+
{
20+
class ApiKeyAuth
21+
{
22+
static void Main(string[] args)
23+
{
24+
new ApiKeyAuth().Run();
25+
}
26+
27+
public void Run()
28+
{
29+
string username = "username";
30+
string apiKey = "api_key";
31+
string secretKey = "secret_key";
32+
string url = "https://whoisxmlapi.com/whoisserver/WhoisService?";
33+
string[] domains =
34+
{
35+
"google.com",
36+
"example.com",
37+
"whoisxmlapi.com",
38+
"twitter.com"
39+
};
40+
bool reuseDigest = true;
41+
42+
this.PerformRequest(username, apiKey, secretKey,
43+
url, domains, reuseDigest);
44+
}
45+
46+
protected void PerformRequest(string username, string apiKey,
47+
string secretKey, string url, string[] domains, bool reuseDigest)
48+
{
49+
long timestamp = this.GetTimeStamp();
50+
string digest = this.GenerateDigest(
51+
username, apiKey, secretKey, timestamp
52+
);
53+
54+
foreach (string domain in domains)
55+
{
56+
try
57+
{
58+
if (!reuseDigest)
59+
{
60+
timestamp = this.GetTimeStamp();
61+
digest = this.GenerateDigest(
62+
username, apiKey, secretKey, timestamp
63+
);
64+
}
65+
string request = this.BuildRequest(
66+
username, timestamp, digest, domain
67+
);
68+
string response = this.GetWhoisData(url + request);
69+
if (response.IndexOf("Request timeout") > -1)
70+
{
71+
timestamp = this.GetTimeStamp();
72+
digest = this.GenerateDigest(
73+
username, apiKey, secretKey, timestamp
74+
);
75+
request = this.BuildRequest(
76+
username, timestamp, digest, domain
77+
);
78+
response = this.GetWhoisData(url + request);
79+
}
80+
this.PrintResponse(response);
81+
} catch(Exception e)
82+
{
83+
Console.WriteLine(
84+
"Error occurred\r\nCannot get whois data for "
85+
+ domain
86+
);
87+
}
88+
}
89+
Console.ReadLine();
90+
}
91+
92+
protected string GenerateDigest(
93+
string username, string apiKey, string secretKey, long timestamp
94+
)
95+
{
96+
string data = username + timestamp + apiKey;
97+
HMACMD5 hmac = new HMACMD5(Encoding.UTF8.GetBytes(secretKey));
98+
string hex = BitConverter.ToString(
99+
hmac.ComputeHash(Encoding.UTF8.GetBytes(data))
100+
);
101+
return hex.Replace("-", "").ToLower();
102+
}
103+
104+
protected string BuildRequest(
105+
string username, long timestamp, string digest, string domain
106+
)
107+
{
108+
UserData ud = new UserData();
109+
ud.u = username;
110+
ud.t = timestamp;
111+
string userData = JsonConvert.SerializeObject(ud, Formatting.None);
112+
var userDataBytes = System.Text.Encoding.UTF8.GetBytes(userData);
113+
string userDataBase64 = System.Convert.ToBase64String(userDataBytes);
114+
115+
StringBuilder requestString = new StringBuilder();
116+
requestString.Append("requestObject=");
117+
requestString.Append(Uri.EscapeDataString(userDataBase64));
118+
requestString.Append("&digest=");
119+
requestString.Append(Uri.EscapeDataString(digest));
120+
requestString.Append("&domainName=");
121+
requestString.Append(domain);
122+
requestString.Append("&outputFormat=json");
123+
124+
return requestString.ToString();
125+
}
126+
127+
protected string GetWhoisData(string url)
128+
{
129+
string response;
130+
try {
131+
WebRequest wr = WebRequest.Create(url);
132+
WebResponse wp = wr.GetResponse();
133+
134+
using (Stream data = wp.GetResponseStream())
135+
{
136+
using (StreamReader reader = new StreamReader(data))
137+
{
138+
response = reader.ReadToEnd();
139+
}
140+
}
141+
wp.Close();
142+
} catch (Exception e)
143+
{
144+
Console.WriteLine(e.Message);
145+
throw e;
146+
}
147+
return response;
148+
}
149+
150+
protected void PrintResponse(string response)
151+
{
152+
dynamic responseObject = JsonConvert.DeserializeObject(response);
153+
154+
if (responseObject.WhoisRecord != null)
155+
{
156+
var whois = responseObject.WhoisRecord;
157+
if (whois.createdDate != null)
158+
{
159+
Console.WriteLine(
160+
"Created date: "
161+
+ whois.createdDate.ToString()
162+
);
163+
}
164+
if (whois.expiresDate != null)
165+
{
166+
Console.WriteLine(
167+
"Expires date: "
168+
+ whois.expiresDate.ToString()
169+
);
170+
}
171+
if (whois.domainName != null)
172+
{
173+
Console.WriteLine(
174+
"Domain name: "
175+
+ whois.domainName.ToString()
176+
);
177+
}
178+
Console.WriteLine("--------------------------------");
179+
return;
180+
}
181+
182+
Console.WriteLine(response);
183+
}
184+
185+
protected long GetTimeStamp()
186+
{
187+
return (long)(DateTime.Now.Subtract(
188+
new DateTime(1970, 1, 1)
189+
).TotalMilliseconds * 1000);
190+
}
191+
}
192+
193+
class UserData
194+
{
195+
public string u { get; set; }
196+
public long t { get; set; }
197+
}
198+
}

0 commit comments

Comments
 (0)