You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpSender.cs 1.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Net;
  4. using System.Net.Http.Json;
  5. using System.Text;
  6. namespace Server
  7. {
  8. class HttpSender
  9. {
  10. private string url;
  11. private string token;
  12. public HttpSender(string url, string token)
  13. {
  14. this.url = url;
  15. this.token = token;
  16. }
  17. // void Test()
  18. // {
  19. // this.SendHttpRequest(new()).Wait();
  20. // }
  21. public async Task SendHttpRequest(JObject body)
  22. {
  23. try
  24. {
  25. var request = new HttpClient();
  26. request.DefaultRequestHeaders.Authorization = new("Bearer", token);
  27. using (var response = await request.PutAsync(url, JsonContent.Create(new
  28. {
  29. result = new TeamScore[]
  30. {
  31. new TeamScore() { team_name = "Student", score = 0, },
  32. new TeamScore() { team_name = "Tricker", score = 0, },
  33. }
  34. })))
  35. {
  36. Console.WriteLine("Send to web successfully!");
  37. Console.WriteLine($"Web response: {await response.Content.ReadAsStringAsync()}");
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. Console.WriteLine("Fail to send msg to web!");
  43. Console.WriteLine(e);
  44. }
  45. }
  46. }
  47. internal class TeamScore
  48. {
  49. public string team_name { get; set; } = "";
  50. public int score { get; set; } = 0;
  51. }
  52. }