This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCall.cs
94 lines (76 loc) · 2.51 KB
/
Call.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Specialized;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace WHMCS
{
public class Call
{
private readonly string Username;
private readonly string Password;
private readonly string AccessKey;
private readonly string Url;
public Call(string Username, string Password, string AccessKey, string Url)
{
this.Username = Username;
this.Password = CalculateMD5Hash(Password);
this.AccessKey = AccessKey;
this.Url = Url + "/includes/api.php";
}
private NameValueCollection BuildRequestData(NameValueCollection data)
{
NameValueCollection request = new NameValueCollection()
{
{ "username", Username},
{ "password", Password},
{ "accesskey", AccessKey },
{ "responsetype", "json"}
};
foreach(string key in data)
{
request.Add(key, data[key]);
}
return request;
}
public string MakeCall(NameValueCollection data)
{
byte[] webResponse;
try
{
webResponse = new WebClient().UploadValues(Url, BuildRequestData(data));
}
catch (Exception ex)
{
throw new Exception("Unable to connect to WHMCS API. " + ex.Message.ToString());
}
return Encoding.ASCII.GetString(webResponse);
}
public async Task<string> MakeCallAsync(NameValueCollection data)
{
byte[] webResponse;
try
{
webResponse = await new WebClient().UploadValuesTaskAsync(Url, data);
}
catch (Exception ex)
{
throw new Exception("Unable to connect to WHMCS API. " + ex.Message.ToString());
}
return Encoding.ASCII.GetString(webResponse);
}
private string CalculateMD5Hash(string input)
{
MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
foreach (byte t in hash)
{
sb.Append(t.ToString("x2"));
}
return sb.ToString();
}
}
}