@@ -7,6 +7,17 @@ namespace CypherNet.Http
7
7
{
8
8
public class WebClient : IWebClient
9
9
{
10
+ private readonly BasicAuthCredentials _credentials ;
11
+
12
+ public WebClient ( )
13
+ {
14
+ }
15
+
16
+ public WebClient ( BasicAuthCredentials credentials )
17
+ {
18
+ _credentials = credentials ;
19
+ }
20
+
10
21
#region IWebClient Members
11
22
12
23
public async Task < IHttpResponseMessage > GetAsync ( string url )
@@ -31,15 +42,9 @@ public async Task<IHttpResponseMessage> DeleteAsync(string url)
31
42
32
43
#endregion
33
44
34
- private async Task < IHttpResponseMessage > Execute ( string url , HttpMethod method )
45
+ private Task < IHttpResponseMessage > Execute ( string url , HttpMethod method )
35
46
{
36
- var msg = new HttpRequestMessage ( method , url ) ;
37
-
38
- using ( var client = new HttpClient ( ) )
39
- {
40
- var result = await client . SendAsync ( msg ) . ConfigureAwait ( false ) ;
41
- return new HttpResponseMessageWrapper ( result ) ;
42
- }
47
+ return Execute ( url , null , method ) ;
43
48
}
44
49
45
50
private async Task < IHttpResponseMessage > Execute ( string url , String content , HttpMethod method )
@@ -54,10 +59,14 @@ private async Task<IHttpResponseMessage> Execute(string url, String content, Htt
54
59
55
60
using ( var client = new HttpClient ( ) )
56
61
{
62
+ if ( _credentials != null )
63
+ {
64
+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Basic" , _credentials . EncodedCredentials ) ;
65
+ }
66
+
57
67
var result = await client . SendAsync ( msg ) . ConfigureAwait ( false ) ;
58
68
return new HttpResponseMessageWrapper ( result ) ;
59
69
}
60
70
}
61
71
}
62
-
63
72
}
0 commit comments