RdHttpClient.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. namespace Ragdoll
  6. {
  7. public class RdHttpClient : ScriptSingleton<RdHttpClient>
  8. {
  9. private readonly HttpClient _httpClient;
  10. public RdHttpClient()
  11. {
  12. _httpClient = new HttpClient();
  13. _httpClient.Timeout = new TimeSpan(0, 0, 0, 5, 0);
  14. }
  15. public async Task<string> PostAsync(string url, string content)
  16. {
  17. Debug.Log("http-post-async-request ===== " + url + " ===== " + content);
  18. HttpContent httpContent = new StringContent(content);
  19. try
  20. {
  21. var response = await _httpClient.PostAsync(url, httpContent);
  22. var responseContent = await response.Content.ReadAsStringAsync();
  23. Debug.Log("http-post-async-result ===== " + url + "/resp ===== " + responseContent);
  24. return responseContent;
  25. }
  26. catch (Exception e)
  27. {
  28. Debug.Log("http-post-async-result ===== " + url + "/err ===== " + e);
  29. return null;
  30. }
  31. }
  32. public async Task<string> GetAsync(string url)
  33. {
  34. Debug.Log("http-get-async-request ===== " + url + " ===== ");
  35. try
  36. {
  37. var response = await _httpClient.GetAsync(url);
  38. var responseContent = await response.Content.ReadAsStringAsync();
  39. Debug.Log("http-get-async-result ===== " + url + "/resp ===== " + responseContent);
  40. return responseContent;
  41. }
  42. catch (Exception e)
  43. {
  44. Debug.Log("http-get-async-result ===== " + url + "/err ===== " + e);
  45. return null;
  46. }
  47. }
  48. }
  49. }