7
7
using System . Text ;
8
8
using System . IO ;
9
9
using HtmlAgilityPack ;
10
+ using System . Net . Http ;
11
+ using System . Reflection . Metadata ;
12
+ using System . Net . Http . Headers ;
13
+ using static System . Net . WebRequestMethods ;
14
+ using static System . Runtime . InteropServices . JavaScript . JSType ;
15
+ using System . Reflection . PortableExecutable ;
16
+ using System . Xml . XPath ;
10
17
11
18
namespace CLARiNET
12
19
{
13
20
public class WDWebService
14
21
{
22
+ static readonly HttpClient _client = new HttpClient ( ) ;
23
+
15
24
public static byte [ ] CallRest ( string tenant , string username , string password , string url , string method , byte [ ] data )
16
25
{
17
- using ( var webClient = new WebClient ( ) )
18
- {
19
- ServicePointManager . Expect100Continue = true ;
20
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
21
- ServicePointManager . ServerCertificateValidationCallback = delegate { return true ; } ;
22
- if ( ! String . IsNullOrEmpty ( username ) )
23
- {
24
- webClient . Credentials = new NetworkCredential ( username , password ) ;
25
- webClient . Headers . Add ( "X-Originator" , "CLARiNET" ) ;
26
- webClient . Headers . Add ( "X-Tenant" , tenant ) ;
27
- if ( method == WebRequestMethods . Http . Get )
28
- {
29
- return webClient . DownloadData ( url ) ;
30
- }
31
- else
32
- {
33
- return webClient . UploadData ( url , data ) ;
34
- }
35
- }
26
+ HttpRequestMessage http = new HttpRequestMessage ( ) ;
27
+ http . Headers . Add ( "X-Originator" , "CLARiNET" ) ;
28
+ http . Headers . Add ( "X-Tenant" , tenant ) ;
29
+ http . RequestUri = new Uri ( url ) ;
30
+ http . Method = new HttpMethod ( method ) ;
31
+ http . BasicAuth ( username , password ) ;
32
+
33
+ if ( method != WebRequestMethods . Http . Get )
34
+ {
35
+ http . Content = new ByteArrayContent ( data ) ;
36
36
}
37
- return null ;
37
+
38
+ HttpResponseMessage response = _client . Send ( http ) ;
39
+ return response . Content . ReadAsByteArrayAsync ( ) . Result ;
40
+
38
41
}
39
42
40
43
public static string WrapSOAP ( string username , string password , string xmlBody )
@@ -120,10 +123,13 @@ public static Dictionary<string, string> Download(string url)
120
123
string html = "" ;
121
124
HtmlAgilityPack . HtmlDocument htmlDoc = new HtmlAgilityPack . HtmlDocument ( ) ;
122
125
123
- using ( var webClient = new WebClient ( ) )
124
- {
125
- html = webClient . DownloadString ( url ) ;
126
- }
126
+ HttpRequestMessage http = new HttpRequestMessage ( ) ;
127
+ http . RequestUri = new Uri ( url ) ;
128
+
129
+
130
+ HttpResponseMessage response = _client . Send ( http ) ;
131
+ html = response . Content . ReadAsStringAsync ( ) . Result ;
132
+
127
133
htmlDoc . LoadHtml ( html ) ;
128
134
129
135
HtmlNodeCollection nodes = htmlDoc . DocumentNode . SelectNodes ( "//a[contains(@href, '.xsd')]" ) ;
@@ -144,60 +150,67 @@ public static Dictionary<string, string> Load(string data)
144
150
145
151
public static string GetServiceURL ( string envURL , string tenant , string username , string password )
146
152
{
147
- string result = "" ;
148
153
149
- using ( var webClient = new WebClient ( ) )
150
- {
151
- ServicePointManager . Expect100Continue = true ;
152
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
153
- ServicePointManager . ServerCertificateValidationCallback = delegate { return true ; } ;
154
- webClient . Credentials = new NetworkCredential ( username + "@" + tenant , password ) ;
155
- result = webClient . DownloadString ( envURL + "/cc-cloud-master/service-gateway" ) ;
156
- }
154
+ HttpRequestMessage http = new HttpRequestMessage ( ) ;
155
+ http . RequestUri = new Uri ( envURL + "/cc-cloud-master/service-gateway" ) ;
156
+ http . BasicAuth ( username + "@" + tenant , password ) ;
157
+
158
+ HttpResponseMessage response = _client . Send ( http ) ;
159
+ return response . Content . ReadAsStringAsync ( ) . Result ;
157
160
158
- return result ;
159
161
}
160
162
161
163
public static string CallAPI ( string username , string password , string url , string xmlData )
162
164
{
163
165
try
164
166
{
165
- using ( var webClient = new WebClient ( ) )
166
- {
167
- webClient . Headers . Add ( "Content-Type" , "text/xml; charset=utf-8" ) ;
168
- ServicePointManager . Expect100Continue = true ;
169
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
170
- ServicePointManager . ServerCertificateValidationCallback = delegate { return true ; } ;
171
- webClient . Credentials = new NetworkCredential ( username , password ) ;
172
- byte [ ] data = Encoding . UTF8 . GetBytes ( WDWebService . WrapSOAP ( username , password , xmlData ) ) ;
173
- byte [ ] rData = webClient . UploadData ( url , data ) ;
174
-
175
- return new XDeclaration ( "1.0" , "UTF-8" , null ) . ToString ( ) + Environment . NewLine + XDocument . Parse ( Encoding . UTF8 . GetString ( rData ) ) . ToString ( ) + Environment . NewLine ;
176
- }
177
- }
178
- catch ( WebException webEx )
179
- {
180
- String responseFromServer = webEx . Message . ToString ( ) + Environment . NewLine ;
181
- if ( webEx . Response != null )
167
+ HttpRequestMessage http = new HttpRequestMessage ( ) ;
168
+
169
+ http . RequestUri = new Uri ( url ) ;
170
+ http . Method = new HttpMethod ( WebRequestMethods . Http . Post ) ;
171
+ http . BasicAuth ( username , password ) ;
172
+
173
+
174
+ byte [ ] data = Encoding . UTF8 . GetBytes ( WDWebService . WrapSOAP ( username , password , xmlData ) ) ;
175
+ http . Content = new ByteArrayContent ( data ) ;
176
+ http . Content . Headers . Add ( "Content-Type" , "text/xml; charset=utf-8" ) ;
177
+
178
+ HttpResponseMessage response = _client . Send ( http ) ;
179
+
180
+ if ( ! response . IsSuccessStatusCode )
182
181
{
183
- using ( WebResponse response = webEx . Response )
182
+ try
184
183
{
185
- Stream dataRs = response . GetResponseStream ( ) ;
186
- using ( StreamReader reader = new StreamReader ( dataRs ) )
184
+ string result = response . Content . ReadAsStringAsync ( ) . Result ;
185
+ var xDoc = XDocument . Parse ( result ) ;
186
+ XmlNamespaceManager ns = new XmlNamespaceManager ( new NameTable ( ) ) ;
187
+ ns . AddNamespace ( "wd" , "urn:com.workday/bsvc" ) ;
188
+ if ( xDoc != null )
187
189
{
188
- try
189
- {
190
- responseFromServer += XDocument . Parse ( reader . ReadToEnd ( ) ) ;
191
- }
192
- catch
193
- {
194
- // ignore exception
195
- }
190
+ result = xDoc . XPathSelectElement ( "//faultstring" , ns ) . Value ;
196
191
}
192
+ return result ;
197
193
}
194
+ catch
195
+ {
196
+ // ignore exception
197
+ }
198
+ return null ;
199
+
198
200
}
201
+
202
+ byte [ ] rData = response . Content . ReadAsByteArrayAsync ( ) . Result ;
203
+ return new XDeclaration ( "1.0" , "UTF-8" , null ) . ToString ( ) + Environment . NewLine + XDocument . Parse ( Encoding . UTF8 . GetString ( rData ) ) . ToString ( ) + Environment . NewLine ;
204
+
205
+ }
206
+ catch ( HttpRequestException webEx )
207
+ {
208
+
209
+ string responseFromServer = webEx . Message . ToString ( ) + Environment . NewLine ;
199
210
return responseFromServer ;
200
211
}
201
212
}
213
+
214
+
202
215
}
203
216
}
0 commit comments