Browse Source

完成v1.0

liuyuqi-dellpc 3 years ago
parent
commit
49c8117e1d

+ 19 - 2
RESTClient/MainWindow.xaml

@@ -5,8 +5,25 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:RESTClient"
         mc:Ignorable="d"
-        Title="MainWindow" Height="350" Width="525">
+        Title="MainWindow" Height="450" Width="800">
     <Grid>
-        
+        <!--选择框-->
+        <ComboBox x:Name="requestMethodInput" HorizontalAlignment="Left" Margin="10,100,0,0" VerticalAlignment="Top" Width="120" SelectedIndex="0">
+            <ComboBoxItem Content="GET"/>
+            <ComboBoxItem Content="POST"/>
+            <ComboBoxItem Content="PUT"/>
+            <ComboBoxItem Content="DELETE"/>
+        </ComboBox>
+        <Label Content="Request Method" HorizontalAlignment="Left" Margin="10,69,0,0" VerticalAlignment="Top"/>
+        <Label Content="Target URI" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
+        <TextBox x:Name="targetUrlInput" HorizontalAlignment="Left" Height="23" Margin="10,41,0,0" TextWrapping="Wrap" Text="localhost" VerticalAlignment="Top" Width="120"/>
+        <Label Content="Response" HorizontalAlignment="Left" Margin="10,127,0,0" VerticalAlignment="Top"/>
+
+        <TextBox x:Name="responseOutput" HorizontalAlignment="Left" Height="226" Margin="10,158,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="772" AcceptsReturn="True" AcceptsTab="True"/>
+        <Label Content="Request Body" HorizontalAlignment="Left" Margin="135,10,0,0" VerticalAlignment="Top"/>
+ 
+        <TextBox x:Name="requestBodyInput" HorizontalAlignment="Left" Height="112" Margin="135,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="647" Text="{}{ }" IsReadOnly="True"/>
+        <Button x:Name="sendRequestButton" Content="Send Request" HorizontalAlignment="Left" Margin="10,389,0,0" VerticalAlignment="Top" Width="85" Click="OnSendRequestButtonClicked"/>
+        <Label x:Name="responseCodeLabel" Content="Response Code: 000 (NAME)" HorizontalAlignment="Left" Margin="200,10,0,0" VerticalAlignment="Top" Width="582" HorizontalContentAlignment="Right"/>
     </Grid>
 </Window>

+ 33 - 11
RESTClient/MainWindow.xaml.cs

@@ -1,17 +1,7 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
+using System.Net.Http;
 using System.Text;
-using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Navigation;
-using System.Windows.Shapes;
 
 namespace RESTClient
 {
@@ -24,5 +14,37 @@ namespace RESTClient
         {
             InitializeComponent();
         }
+
+        private void OnSendRequestButtonClicked(object sender, RoutedEventArgs e)
+        {
+            string targetUrl = targetUrlInput.Text.Trim();
+            RequestMethod requestMethod = (RequestMethod)requestMethodInput.SelectedIndex;
+            ByteArrayContent requestBody = new ByteArrayContent(
+                Encoding.UTF8.GetBytes(requestBodyInput.Text)
+            );
+
+            SendRequest(targetUrl, requestMethod, requestBody, responseOutput, responseCodeLabel);
+        }
+
+        private async void SendRequest(string target, RequestMethod method, HttpContent body, TextBox contentOutput, Label statusCodeOutput)
+        {
+            RequestManager manager = RequestManager.Get();
+            RequestManager.Response response = await manager.SendRequest(MakeAbsolute(target), method, body);
+            contentOutput.Text = response.content;
+            statusCodeOutput.Content = $"Response Code: {response.statusCodeName} ({response.statusCode}).";
+        }
+
+        private string MakeAbsolute(string url)
+        {
+            if (url.StartsWith("https://") || url.StartsWith("http://"))
+            {
+                return url;
+            }
+            else
+            {
+                return $"https://{url}";
+            }
+        }
+
     }
 }

+ 17 - 0
RESTClient/Model/Request.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace RESTClient.Model
+{
+    class Request
+    {
+        String method;//post,get,put,delete
+        String body;
+        String header;
+        String response;
+
+    }
+}

+ 10 - 0
RESTClient/RESTClient.csproj

@@ -54,6 +54,11 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
+    <Compile Include="Model\Request.cs" />
+    <Compile Include="Utils\RequestManager.cs" />
+    <Compile Include="Views\AboutMe.xaml.cs">
+      <DependentUpon>AboutMe.xaml</DependentUpon>
+    </Compile>
     <Page Include="MainWindow.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
@@ -66,6 +71,10 @@
       <DependentUpon>MainWindow.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
+    <Page Include="Views\AboutMe.xaml">
+      <SubType>Designer</SubType>
+      <Generator>MSBuild:Compile</Generator>
+    </Page>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Properties\AssemblyInfo.cs">
@@ -93,5 +102,6 @@
   <ItemGroup>
     <None Include="App.config" />
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 83 - 0
RESTClient/Utils/RequestManager.cs

@@ -0,0 +1,83 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace RESTClient
+{
+    public enum RequestMethod
+    {
+        GET,
+        POST,
+        PUT,
+        DELETE,
+    }
+
+    public class RequestManager
+    {
+        public struct Response
+        {
+            public int statusCode;
+            public string statusCodeName;
+            public string content;
+        }
+
+        private static RequestManager singleton = null;
+
+        public static RequestManager Get()
+        {
+            return singleton = new RequestManager();
+        }
+
+        private HttpClient client;
+
+        private RequestManager()
+        {
+            client = new HttpClient();
+        }
+
+        public async Task<Response> SendRequest(string target, RequestMethod method, HttpContent body)
+        {
+            HttpResponseMessage httpResponse = null;
+
+            try
+            {
+                switch (method)
+                {
+                    case RequestMethod.DELETE:
+                        httpResponse = await client.DeleteAsync(target); break;
+                    case RequestMethod.GET:
+                        httpResponse = await client.GetAsync(target); break;
+                    case RequestMethod.POST:
+                        httpResponse = await client.PostAsync(target, body); break;
+                    case RequestMethod.PUT:
+                        httpResponse = await client.PutAsync(target, body); break;
+                }
+            }
+            catch (Exception e)
+            {
+                return new Response
+                {
+                    statusCode = 0,
+                    statusCodeName = "ERROR",
+                    content = e.Message
+                };
+            }
+
+            Response response = new Response();
+
+            HttpStatusCode responseStatus = httpResponse.StatusCode;
+            response.statusCode = (int)responseStatus;
+            response.statusCodeName = responseStatus.ToString();
+
+            HttpContent responseContent = httpResponse.Content;
+            response.content = await responseContent.ReadAsStringAsync();
+
+            httpResponse.Dispose();
+            return response;
+        }
+    }
+}

+ 12 - 0
RESTClient/Views/AboutMe.xaml

@@ -0,0 +1,12 @@
+<Window x:Class="RESTClient.Views.AboutMe"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:RESTClient.Views"
+        mc:Ignorable="d"
+        Title="AboutMe" Height="300" Width="300">
+    <Grid>
+        <TextBox Name="aboutme" Text="mail: liuyuqi.gov@msn.cn" FontSize="20" Margin="0,119,0,117" HorizontalAlignment="Center"></TextBox>        
+    </Grid>
+</Window>

+ 27 - 0
RESTClient/Views/AboutMe.xaml.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+
+namespace RESTClient.Views
+{
+    /// <summary>
+    /// Interaction logic for AboutMe.xaml
+    /// </summary>
+    public partial class AboutMe : Window
+    {
+        public AboutMe()
+        {
+            InitializeComponent();
+        }
+    }
+}