1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- using System.Net.Http;
- using System.Threading.Tasks;
- using UnityEngine;
- namespace Ragdoll
- {
- public class RdHttpClient : ScriptSingleton<RdHttpClient>
- {
- private readonly HttpClient _httpClient;
- public RdHttpClient()
- {
- _httpClient = new HttpClient();
- _httpClient.Timeout = new TimeSpan(0, 0, 0, 5, 0);
- }
- public async Task<string> PostAsync(string url, string content)
- {
- Debug.Log("http-post-async-request ===== " + url + " ===== " + content);
- HttpContent httpContent = new StringContent(content);
- try
- {
- var response = await _httpClient.PostAsync(url, httpContent);
- var responseContent = await response.Content.ReadAsStringAsync();
- Debug.Log("http-post-async-result ===== " + url + "/resp ===== " + responseContent);
- return responseContent;
- }
- catch (Exception e)
- {
- Debug.Log("http-post-async-result ===== " + url + "/err ===== " + e);
- return null;
- }
- }
- public async Task<string> GetAsync(string url)
- {
- Debug.Log("http-get-async-request ===== " + url + " ===== ");
- try
- {
- var response = await _httpClient.GetAsync(url);
- var responseContent = await response.Content.ReadAsStringAsync();
- Debug.Log("http-get-async-result ===== " + url + "/resp ===== " + responseContent);
- return responseContent;
- }
- catch (Exception e)
- {
- Debug.Log("http-get-async-result ===== " + url + "/err ===== " + e);
- return null;
- }
- }
- }
- }
|