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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(int[] scores)
  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_id = 0, score = scores[0], },
  32. new TeamScore() { team_id = 1, score = scores[1], },
  33. },
  34. mode = 0
  35. })))
  36. {
  37. Console.WriteLine("Send to web successfully!");
  38. Console.WriteLine($"Web response: {await response.Content.ReadAsStringAsync()}");
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. Console.WriteLine("Fail to send msg to web!");
  44. Console.WriteLine(e);
  45. }
  46. }
  47. }
  48. internal class TeamScore
  49. {
  50. public int team_id { get; set; } = 0;
  51. public int score { get; set; } = 0;
  52. }
  53. }