Guest User

Untitled

a guest
Jul 6th, 2023
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6.  
  7. public class Example1 : MonoBehaviour
  8. {
  9. private HttpClient _client = new HttpClient();
  10.  
  11. void Start()
  12. {
  13. _client.BaseAddress = new System.Uri("http://127.0.0.1:8000");
  14. _client.Timeout = new System.TimeSpan(0, 0, 0, 0, 3000);
  15. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  16. GetRequest();
  17. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  18. }
  19.  
  20. private async Task GetRequest()
  21. {
  22. try
  23. {
  24. CancellationTokenSource cts = new CancellationTokenSource();
  25. cts.CancelAfter(3000);
  26. var task = await _client.GetAsync("/somepath", cts.Token);
  27. string result = await task.Content.ReadAsStringAsync();
  28. Debug.Log("GOT:\n" + result);
  29. }
  30. catch (Exception e){
  31. Debug.Log("EXCEPTION ============================");
  32. Debug.Log(e.Message);
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment