-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRequestMain.csx
67 lines (59 loc) · 3.23 KB
/
RequestMain.csx
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
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Collections.Generic;
public string Get(string url, AuthenticationHeaderValue authenticationHeaderValue = null, Dictionary<string, string> customHeaders = null)
{
var httpClient = new HttpClient();
SetOptionalHeaders(httpClient, authenticationHeaderValue, customHeaders);
return httpClient.GetStringAsync(url).Result;
}
public HttpResponseMessage GetJson(string url, AuthenticationHeaderValue authenticationHeaderValue = null, Dictionary<string, string> customHeaders = null)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
SetOptionalHeaders(httpClient, authenticationHeaderValue, customHeaders);
return httpClient.GetAsync(url).Result;
}
public T GetJson<T>(string url, AuthenticationHeaderValue authenticationHeaderValue = null, Dictionary<string, string> customHeaders = null) where T : class
{
var result = GetJson(url, authenticationHeaderValue, customHeaders);
return JsonConvert.DeserializeObject<T>(result.Content.ReadAsStringAsync().Result);
}
public HttpResponseMessage PostJson<T>(string url, T objectToPost, AuthenticationHeaderValue authenticationHeaderValue = null, Dictionary<string, string> customHeaders = null) where T : class
{
var httpClient = new HttpClient();
var bodyContent = new StringContent(JsonConvert.SerializeObject(objectToPost), Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
SetOptionalHeaders(httpClient, authenticationHeaderValue, customHeaders);
return httpClient.PostAsync(url, bodyContent).Result;
}
public HttpResponseMessage PutJson<T>(string url, T objectToPut, AuthenticationHeaderValue authenticationHeaderValue = null, Dictionary<string, string> customHeaders = null) where T : class
{
var httpClient = new HttpClient();
var bodyContent = new StringContent(JsonConvert.SerializeObject(objectToPut), Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
SetOptionalHeaders(httpClient, authenticationHeaderValue, customHeaders);
return httpClient.PutAsync(url, bodyContent).Result;
}
public HttpResponseMessage Delete(string url, AuthenticationHeaderValue authenticationHeaderValue = null, Dictionary<string, string> customHeaders = null)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
SetOptionalHeaders(httpClient, authenticationHeaderValue, customHeaders);
return httpClient.DeleteAsync(url).Result;
}
private void SetOptionalHeaders(HttpClient httpClient, AuthenticationHeaderValue authenticationHeaderValue = null, Dictionary<string, string> customHeaders = null)
{
if (authenticationHeaderValue != null)
{
httpClient.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
}
if (customHeaders != null)
{
foreach (var customHeader in customHeaders)
{
httpClient.DefaultRequestHeaders.Add(customHeader.Key, customHeader.Value);
}
}
}