MainWindow.xaml.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Net.Http;
  2. using System.Text;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace RESTClient
  6. {
  7. /// <summary>
  8. /// Interaction logic for MainWindow.xaml
  9. /// </summary>
  10. public partial class MainWindow : Window
  11. {
  12. public MainWindow()
  13. {
  14. InitializeComponent();
  15. }
  16. private void OnSendRequestButtonClicked(object sender, RoutedEventArgs e)
  17. {
  18. string targetUrl = targetUrlInput.Text.Trim();
  19. RequestMethod requestMethod = (RequestMethod)requestMethodInput.SelectedIndex;
  20. ByteArrayContent requestBody = new ByteArrayContent(
  21. Encoding.UTF8.GetBytes(requestBodyInput.Text)
  22. );
  23. SendRequest(targetUrl, requestMethod, requestBody, responseOutput, responseCodeLabel);
  24. }
  25. private async void SendRequest(string target, RequestMethod method, HttpContent body, TextBox contentOutput, Label statusCodeOutput)
  26. {
  27. RequestManager manager = RequestManager.Get();
  28. RequestManager.Response response = await manager.SendRequest(MakeAbsolute(target), method, body);
  29. contentOutput.Text = response.content;
  30. statusCodeOutput.Content = $"Response Code: {response.statusCodeName} ({response.statusCode}).";
  31. }
  32. private string MakeAbsolute(string url)
  33. {
  34. if (url.StartsWith("https://") || url.StartsWith("http://"))
  35. {
  36. return url;
  37. }
  38. else
  39. {
  40. return $"https://{url}";
  41. }
  42. }
  43. }
  44. }