forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (102 loc) · 3.9 KB
/
Program.cs
File metadata and controls
115 lines (102 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Linq;
using System.Net;
using NetCoreServer;
namespace HttpClient
{
class Program
{
static void Main(string[] args)
{
// HTTP server address
string address = "localhost";
if (args.Length > 0)
address = args[0];
// HTTP server port
int port = 8080;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"HTTP server address: {address}");
Console.WriteLine($"HTTP server port: {port}");
Console.WriteLine();
// Create a new HTTP client
var client = new HttpClientEx(Dns.GetHostAddresses(address).FirstOrDefault(), port);
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Reconnect the client
if (line == "!")
{
Console.Write("Client reconnecting...");
if (client.IsConnected)
client.ReconnectAsync();
else
client.ConnectAsync();
Console.WriteLine("Done!");
continue;
}
var commands = line.Split(' ');
if (commands.Length < 2)
{
Console.WriteLine("HTTP method and URL must be entered!");
continue;
}
if (commands[0].ToUpper() == "HEAD")
{
var response = client.SendHeadRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "GET")
{
var response = client.SendGetRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "POST")
{
if (commands.Length < 3)
{
Console.WriteLine("HTTP method, URL and body must be entered!");
continue;
}
var response = client.SendPostRequest(commands[1], commands[2]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "PUT")
{
if (commands.Length < 3)
{
Console.WriteLine("HTTP method, URL and body must be entered!");
continue;
}
var response = client.SendPutRequest(commands[1], commands[2]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "DELETE")
{
var response = client.SendDeleteRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "OPTIONS")
{
var response = client.SendOptionsRequest(commands[1]).Result;
Console.WriteLine(response);
}
else if (commands[0].ToUpper() == "TRACE")
{
var response = client.SendTraceRequest(commands[1]).Result;
Console.WriteLine(response);
}
else
Console.WriteLine("Unknown HTTP method");
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.Disconnect();
Console.WriteLine("Done!");
}
}
}