Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Http;
- using System.Threading;
- using System.Threading.Tasks;
- using UnityEngine;
- public class Example1 : MonoBehaviour
- {
- private HttpClient _client = new HttpClient();
- void Start()
- {
- _client.BaseAddress = new System.Uri("http://127.0.0.1:8000");
- _client.Timeout = new System.TimeSpan(0, 0, 0, 0, 3000);
- #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- GetRequest();
- #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
- }
- private async Task GetRequest()
- {
- try
- {
- CancellationTokenSource cts = new CancellationTokenSource();
- cts.CancelAfter(3000);
- var task = await _client.GetAsync("/somepath", cts.Token);
- string result = await task.Content.ReadAsStringAsync();
- Debug.Log("GOT:\n" + result);
- }
- catch (Exception e){
- Debug.Log("EXCEPTION ============================");
- Debug.Log(e.Message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment