1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- namespace EInk.Tools
- {
- public enum HttpResultCode
- {
- Ok,
- Fail,
- Error
- }
- public static class HttpHelper
- {
- public static (bool, string) Get(string url, int timeOut = 10)
- {
- try
- {
- using HttpClient client = new();
- client.Timeout = TimeSpan.FromSeconds(1000 * timeOut);
- HttpResponseMessage response = client.GetAsync(url).Result;
- response.EnsureSuccessStatusCode();
- string responseBody = response.Content.ReadAsStringAsync().Result;
- return (true, responseBody);
- }
- catch (HttpRequestException ex)
- {
- return (false, ex.Message);
- }
- }
- public static (bool, string) Post(string url, string inputData, int timeOut = 10)
- {
- try
- {
- using HttpClient client = new();
- client.Timeout = TimeSpan.FromSeconds(1000 * timeOut);
- HttpContent content = new StringContent(inputData, System.Text.Encoding.UTF8, "application/json");
- HttpResponseMessage response = client.PostAsync(url, content).Result;
- response.EnsureSuccessStatusCode();
- string responseBody = response.Content.ReadAsStringAsync().Result;
- return (true, responseBody);
- }
- catch (HttpRequestException ex)
- {
- return (false, ex.Message);
- }
- }
- public static (bool, string) Post(string url, int timeOut = 10)
- {
- return Post(url, "", timeOut);
- }
- }
- }
|