forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHelper.cs
More file actions
56 lines (51 loc) · 1.76 KB
/
HttpHelper.cs
File metadata and controls
56 lines (51 loc) · 1.76 KB
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
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Blog.Core.Common.Helper
{
/// <summary>
/// httpclinet请求方式,请尽量使用IHttpClientFactory方式
/// </summary>
public class HttpHelper
{
public static async Task<string> GetAsync(string serviceAddress)
{
try
{
string result = string.Empty;
Uri getUrl = new Uri(serviceAddress);
using var httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 0, 60);
result = await httpClient.GetAsync(serviceAddress).Result.Content.ReadAsStringAsync();
return result;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return null;
}
public static async Task<string> PostAsync(string serviceAddress, string requestJson = null)
{
try
{
string result = string.Empty;
Uri postUrl = new Uri(serviceAddress);
using (HttpContent httpContent = new StringContent(requestJson))
{
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using var httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 0, 60);
result = await httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync();
}
return result;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return null;
}
}
}