-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWebServiceClient.cs
255 lines (229 loc) · 9.82 KB
/
WebServiceClient.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright (c) 2012-2013 MetricWise, Inc
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Vtiger
{
public delegate void WebServiceExceptionHandler(Exception exception);
public class WebServiceClient
{
public event WebServiceExceptionHandler OnException;
private string serverURL = "";
private string sessionName = "";
public void SetServerUrl(string url)
{
serverURL = url;
}
public JToken DoLogin(string userName, string accessKey)
{
string token = GetChallengeToken(userName);
string accessData = token + accessKey;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["operation"] = "login";
dictionary["username"] = userName;
dictionary["accessKey"] = MD5Hex(accessData);
JToken data = PostRequest(dictionary);
if (data != null)
{
sessionName = data["sessionName"].Value<string>();
}
return data;
}
public JToken DoLoginPassword(string userName, string password)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["operation"] = "login.password";
dictionary["user_name"] = userName;
dictionary["user_password"] = password;
JToken data = PostRequest(dictionary);
if (data != null)
{
sessionName = data["sessionName"].Value<string>();
}
return data;
}
public void DoLogout()
{
if (sessionName.Length > 0)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["operation"] = "logout";
dictionary["sessionName"] = sessionName;
JToken data = GetRequest(dictionary);
if (data != null)
{
sessionName = "";
}
}
}
public JToken DoDescribe(string elementType) {
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["elementType"] = elementType;
dictionary["operation"] = "describe";
dictionary["sessionName"] = sessionName;
return GetRequest(dictionary);
}
public JToken DoQuery(string query)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["operation"] = "query";
dictionary["query"] = query;
dictionary["sessionName"] = sessionName;
return GetRequest(dictionary);
}
public JToken DoSync(string modifiedTime, string elementType)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["operation"] = "sync";
dictionary["sessionName"] = sessionName;
dictionary["modifiedTime"] = modifiedTime;
dictionary["elementType"] = elementType;
return GetRequest(dictionary);
}
public void AsyncUpdate(JToken element)
{
AsyncUpdate(element, null);
}
public void AsyncUpdate(JToken element, WebServiceDataHandler callback)
{
Dictionary<string, string> dictionary = GetUpdateDictionary(element);
WebServiceAsyncRequest request = new WebServiceAsyncRequest();
byte[] postData = ToPostData(dictionary);
request.OnException += OnException;
if (null != callback)
{
request.OnData += callback;
}
request.Begin(serverURL, postData);
}
public JToken DoUpdate(JToken element)
{
Dictionary<string, string> dictionary = GetUpdateDictionary(element);
return PostRequest(dictionary);
}
private Dictionary<string, string> GetUpdateDictionary(JToken element)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["operation"] = "update";
dictionary["sessionName"] = sessionName;
dictionary["element"] = element.ToString();
return dictionary;
}
private string GetChallengeToken(string username)
{
string token = "";
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["operation"] = "getchallenge";
dictionary["username"] = username;
JToken data = GetRequest(dictionary);
if (data != null)
{
token = data["token"].Value<String>();
}
return token;
}
private JToken GetRequest(Dictionary<string, string> dictionary)
{
JToken data = null;
String url = serverURL + "?" + ToGetString(dictionary);
try
{
Action action = delegate()
{
WebRequest request = System.Net.WebRequest.Create(url);
request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();
request.Method = "GET";
data = ParseResponse(request);
};
WebServiceRetry.ExponentialBackoff(action, TimeSpan.FromMilliseconds(512), 3);
}
catch (Exception e)
{
OnException(e);
}
return data;
}
private JToken PostRequest(Dictionary<string, string> dictionary)
{
JToken data = null;
try
{
Action action = delegate()
{
WebRequest request = System.Net.WebRequest.Create(serverURL);
request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();
byte[] postData = ToPostData(dictionary);
request.ContentLength = postData.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
Stream stream = request.GetRequestStream();
stream.Write(postData, 0, postData.Length);
stream.Close();
data = ParseResponse(request);
};
WebServiceRetry.ExponentialBackoff(action, TimeSpan.FromMilliseconds(512), 3);
}
catch (Exception exception)
{
OnException(exception);
}
return data;
}
private byte[] ToPostData(Dictionary<string, string> dictionary)
{
string getString = ToGetString(dictionary);
return Encoding.UTF8.GetBytes(getString);
}
private string ToGetString(Dictionary<string, string> dictionary)
{
StringBuilder query = new StringBuilder();
foreach (string key in dictionary.Keys)
{
string value = dictionary[key];
query.Append(string.Format("{0}={1}", System.Uri.EscapeDataString(key), System.Uri.EscapeDataString(value)));
query.Append("&");
}
return query.ToString();
}
private JToken ParseResponse(WebRequest request)
{
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string json = reader.ReadToEnd();
reader.Close();
response.Close();
return ParseJson(json);
}
private JToken ParseJson(string json) {
JObject data = JObject.Parse(json);
if ("true" != data["success"].ToString())
{
throw new WebServiceException(data["error"]);
}
return (JToken)data["result"];
}
private string MD5Hex(string value)
{
MD5 crypto = MD5.Create();
byte[] hash = crypto.ComputeHash(Encoding.UTF8.GetBytes(value));
StringBuilder hex = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
hex.Append(hash[i].ToString("x2"));
}
return hex.ToString();
}
}
}