-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebClient.cs
138 lines (121 loc) · 3.21 KB
/
WebClient.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
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace WebClient
{
/// <summary>
/// Web client is a wrapper around System.Net.Htp.HttpClient.
/// </summary>
public class WebClient
{
/// <summary>
/// HTTP Handler instance.
/// </summary>
private HttpClientHandler handler;
/// <summary>
/// Request Timeout.
/// </summary>
/// <value>The timeout.</value>
public TimeSpan Timeout { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="WebClient.WebClient"/> class.
/// </summary>
public WebClient ()
{
handler = new HttpClientHandler ();
//handler.PreAuthenticate = true;
handler.UseDefaultCredentials = true;
}
/// <summary>
/// Execute specified HTTP request.
/// </summary>
/// <returns>HTTP Response.</returns>
/// <param name="request">HTTP Request.</param>
public HttpResponseMessage Execute(HttpRequestMessage request) {
return ExecuteAsync (request).Result;
}
/// <summary>
/// Executes a HTTP Get request.
/// </summary>
/// <returns>Data received.</returns>
/// <param name="uri">Request URI.</param>
public Stream ExecuteGet(Uri uri) {
HttpRequestMessage webRequest = new HttpRequestMessage (WebMethod.Get, uri);
webRequest.Headers.Add("Pragma", "no-cache");
HttpResponseMessage webResponse;
MemoryStream remoteStream = new MemoryStream();
try
{
webResponse = Execute(webRequest);
}
catch (Exception e)
{
// TODO: Errorhandling
Debug.WriteLine(e.Message);
return null;
}
if (webResponse == null)
{
// TODO: Errorhandling
Debug.WriteLine("Empty WebResponse " + Environment.NewLine + uri + Environment.NewLine);
return null;
}
try
{
// Get the stream object of the response
webResponse.Content.ReadAsStreamAsync().Result.CopyTo(remoteStream);
}
catch (Exception e)
{
// TODO: Errorhandling
Debug.WriteLine(e.Message);
return null;
}
return remoteStream;
}
/// <summary>
/// Executes a HTTP reqquest async.
/// </summary>
/// <returns>HTTP Response.</returns>
/// <param name="request">HTTP Request.</param>
public async Task<HttpResponseMessage> ExecuteAsync(HttpRequestMessage request) {
HttpClient client = new HttpClient (handler, false);
client.Timeout = this.Timeout;
var response = await client.SendAsync(request).ConfigureAwait(false);
return response;
}
/// <summary>
/// Gets or sets authentication information used by the web client.
/// </summary>
/// <value>authentication information</value>
public ICredentials Credentials {
get {
return handler.Credentials;
}
set {
try {
handler.Credentials = value;
handler.UseDefaultCredentials = (handler.Credentials == null) ? true : false;
} catch (Exception ex) {
Debug.WriteLine (ex.Message);
}
}
}
/// <summary>
/// Gets or sets proxy information used by the web client.
/// </summary>
/// <value>proxy information</value>
public IWebProxy Proxy {
get {
return handler.Proxy;
}
set {
handler.Proxy = value;
handler.UseProxy = (handler.Proxy != null) ? true : false;
}
}
}
}