This repository has been archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttp.cs
55 lines (46 loc) · 1.82 KB
/
Http.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
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Auto_Invitation
{
public class Http
{
public static void Initialize(string token)
{
UpdateGitHubAuthorization(token);
_httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
_httpClient.DefaultRequestHeaders.Add("User-Agent", ".NET Core SUper");
}
public static void UpdateGitHubAuthorization(string value)
=> UpdateAuthorization("token " + value);
public static void UpdateAuthorization(string value)
{
_httpClient.DefaultRequestHeaders.Remove("Authorization");
_httpClient.DefaultRequestHeaders.Add("Authorization", value);
}
private static HttpClient _httpClient = new HttpClient()
{
Timeout = TimeSpan.FromSeconds(5)
};
public static async Task<string> GetStrAsync(string url)
{
HttpResponseMessage hrm = await _httpClient.GetAsync(url);
return await hrm.Content.ReadAsStringAsync();
}
public static async Task<HttpResponseModel<string>> PostStrAsync(string url, string data)
{
var response = await _httpClient.PostAsync(url, new StringContent(data));
return new HttpResponseModel<string>()
{
Status = response.StatusCode,
Data = response.Content.ReadAsStringAsync().Result,
};
}
public static async Task<string> PostStrAsync(string url, string data, int status)
{
var response = await _httpClient.PostAsync(url, new StringContent(data));
return response.Content.ReadAsStringAsync().Result;
}
}
}