-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClientWrapper.cs
116 lines (104 loc) · 4.73 KB
/
HttpClientWrapper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace SimpleWebClient
{
public class HttpClientWrapper
{
public static HttpResponse Send(string serviceUrl, string acceptHeader, HttpMethod httpMethod, string postBody, string postContentType, string username, string password)
{
return ExecuteSend(serviceUrl, acceptHeader, httpMethod, postBody, postContentType, username, password);
}
public static HttpResponse Send(string serviceUrl, string acceptHeader, HttpMethod httpMethod, string postBody, string postContentType)
{
return ExecuteSend(serviceUrl, acceptHeader, httpMethod, postBody, postContentType, null, null);
}
private static HttpResponse ExecuteSend(string serviceUrl, string acceptHeader, HttpMethod httpMethod, string postBody, string postContentType, string username, string password)
{
using (var httpClient = new HttpClient())
{
if (!string.IsNullOrEmpty(username))
{
AddCustomHeaders(AuthenticationHeader(username, password), httpClient);
}
if (!string.IsNullOrEmpty(acceptHeader))
{
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(acceptHeader));
}
if (httpMethod.Method == HttpMethod.Post.Method)
{
//POST
var response = httpClient.PostAsync(serviceUrl, new StringContent(postBody, Encoding.UTF8, postContentType)).Result;
return new HttpResponse
{
Content = response.Content.ReadAsStringAsync().Result,
Status = (int)response.StatusCode,
Success = response.IsSuccessStatusCode
};
}
if (httpMethod.Method == HttpMethod.Get.Method)
{
//GET
var response = httpClient.GetAsync(serviceUrl).Result;
return new HttpResponse
{
Content = response.Content.ReadAsStringAsync().Result,
Status = (int)response.StatusCode,
Success = response.IsSuccessStatusCode
};
}
if (httpMethod.Method == HttpMethod.Put.Method)
{
//PUT
var response = httpClient.PutAsync(serviceUrl, new StringContent(postBody, Encoding.UTF8, postContentType)).Result;
return new HttpResponse
{
Content = response.Content.ReadAsStringAsync().Result,
Status = (int)response.StatusCode,
Success = response.IsSuccessStatusCode
};
}
if (httpMethod.Method == HttpMethod.Delete.Method)
{
//PUT
var response = httpClient.DeleteAsync(serviceUrl).Result;
return new HttpResponse
{
Content = response.Content.ReadAsStringAsync().Result,
Status = (int)response.StatusCode,
Success = response.IsSuccessStatusCode
};
}
throw new Exception(string.Format("[{0}] unsupported method!", httpMethod.Method));
}
}
private static void AddCustomHeaders(Dictionary<string, string> customHeaders, HttpClient client)
{
if (customHeaders == null) return;
foreach (var customHeader in customHeaders)
{
client.DefaultRequestHeaders.TryAddWithoutValidation(customHeader.Key, customHeader.Value);
}
}
private static Dictionary<string, string> AuthenticationHeader(string username, string password)
{
var credentials = Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));
var authenticationHeader = new Dictionary<string, string>
{
{"Authorization", string.Format("Basic {0}", credentials)}
};
return authenticationHeader;
}
}
public class HttpResponse
{
public string Content { get; set; }
public int Status { get; set; }
public bool Success { get; set; }
}
}