-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmMain.cs
280 lines (248 loc) · 9.96 KB
/
frmMain.cs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Net.Http.Headers;
namespace ReSTClient
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
MoveControls();
}
void FillResponseHeader(HttpResponseHeaders headers)
{
//empty the grid
int nCount = dataGridViewHeader.Rows.Count();
//fill the grid from response
int nRows = 0;
var enum1 = headers.GetEnumerator();
while (enum1.MoveNext())
{
KeyValuePair<string, IEnumerable<string>> kvPair = enum1.Current;
dataGridViewHeader.Rows.Add();
dataGridViewHeader.Rows[nRows].Cells[0].Value = kvPair.Key;
var enum2 = headers.GetValues(kvPair.Key).GetEnumerator();
while (enum2.MoveNext())
{
dataGridViewHeader.Rows[nRows++].Cells[1].Value = enum2.Current;
}
}
}
void MoveControls()
{
// tab control position
tabControl1.Top = 50;
tabControl1.Left = 5;
tabControl1.Width = this.Width - 20;
tabControl1.Height = this.Height - 60;
rtbResponse.Top = 5;
rtbResponse.Left = 5;
rtbResponse.Width = tabControl1.Width - 40;
rtbResponse.Height = tabControl1.Height - 70;
dataGridViewHeader.Top = 5;
dataGridViewHeader.Left = 5;
dataGridViewHeader.Width = tabControl1.Width - 40; ;
dataGridViewHeader.Height = tabControl1.Height - 70;
comboBox1.Left = 5;
comboBox1.Top = 5;
comboBox1.Height = 40;
comboBox1.Width = 95;
txtURI.Left = 105;
txtURI.Top = 5;
txtURI.Height = 40;
txtURI.Width = this.Width - 200;
button1.Left = this.Width - 85;
button1.Top = 5;
button1.Height = 40;
button1.Width = 60;
listHistory.Top = 5;
listHistory.Left = 5;
listHistory.Width = this.Width - 40;
listHistory.Height = tabControl1.Height - 70;
}
private void OnSize(object sender, EventArgs e)
{
MoveControls();
}
private void OnSend(object sender, EventArgs e)
{
switch (comboBox1.SelectedIndex)
{
case 0: //GET
getRequest();
break;
case 1: //PUT
putRequest();
break;
case 2: //POST
postRequest();
break;
case 3: //DELETE
deleteRequest();
break;
case 4: //INTERNET Mode
internetRequest();
return;
}
tabControl1.SelectedIndex = 0;
}
private void SetLink(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel link = (LinkLabel) sender;
txtURI.Text = link.Text;
}
private async void getRequest()
{
try
{
string responseBodyAsText;
string url_string = txtURI.Text;
HttpClient httpClient = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(txtUserName.Text + ":" + txtPassword.Text);
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
httpClient.DefaultRequestHeaders.Authorization = header;
HttpResponseMessage response = await httpClient.GetAsync(url_string);
response.EnsureSuccessStatusCode();
responseBodyAsText = await response.Content.ReadAsStringAsync();
// responseBodyAsText = responseBodyAsText.Replace("<br>", Environment.NewLine); // Insert new lines
rtbResponse.Text = responseBodyAsText;
xmlView.Navigate(url_string);
// fill response headers
FillResponseHeader(response.Headers);
}
catch (HttpRequestException hre)
{
rtbResponse.Text = hre.ToString();
}
catch (Exception ex)
{
rtbResponse.Text = ex.ToString();
}
}
private async void putRequest()
{
try{
string responseBodyAsText;
string url_string = txtURI.Text;
HttpClient httpClient = new HttpClient();
HttpContent httpContent = new StringContent(rtbRequest.Rtf);
var byteArray = Encoding.ASCII.GetBytes(txtUserName.Text + ":" + txtPassword.Text);
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
httpClient.DefaultRequestHeaders.Authorization = header;
HttpResponseMessage response = await httpClient.PutAsync(url_string, httpContent);
response.EnsureSuccessStatusCode();
responseBodyAsText = await response.Content.ReadAsStringAsync();
rtbResponse.Text = responseBodyAsText;
xmlView.Navigate(url_string);
}
catch (HttpRequestException hre)
{
rtbResponse.Text = hre.ToString();
}
catch (Exception ex)
{
rtbResponse.Text = ex.ToString();
}
}
private async void postRequest()
{
try
{
string responseBodyAsText;
string url_string = txtURI.Text;
HttpClient httpClient = new HttpClient();
HttpContent httpContent = new StringContent(rtbRequest.Rtf);
var byteArray = Encoding.ASCII.GetBytes(txtUserName.Text + ":" + txtPassword.Text);
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
httpClient.DefaultRequestHeaders.Authorization = header;
HttpResponseMessage response = await httpClient.PostAsync(url_string, httpContent);
response.EnsureSuccessStatusCode();
responseBodyAsText = await response.Content.ReadAsStringAsync();
// responseBodyAsText = responseBodyAsText.Replace("<br>", Environment.NewLine); // Insert new lines
rtbResponse.Text = responseBodyAsText;
xmlView.Navigate(url_string);
}
catch (HttpRequestException hre)
{
rtbResponse.Text = hre.ToString();
}
catch (Exception ex)
{
rtbResponse.Text = ex.ToString();
}
}
private async void deleteRequest()
{
try
{
string responseBodyAsText;
string url_string = txtURI.Text;
HttpClient httpClient = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(txtUserName.Text + ":" + txtPassword.Text);
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
httpClient.DefaultRequestHeaders.Authorization = header;
HttpResponseMessage response = await httpClient.DeleteAsync(url_string);
response.EnsureSuccessStatusCode();
responseBodyAsText = await response.Content.ReadAsStringAsync();
// responseBodyAsText = responseBodyAsText.Replace("<br>", Environment.NewLine); // Insert new lines
rtbResponse.Text = responseBodyAsText;
xmlView.Navigate(url_string);
}
catch (HttpRequestException hre)
{
rtbResponse.Text = hre.ToString();
}
catch (Exception ex)
{
rtbResponse.Text = ex.ToString();
}
}
private void internetRequest()
{
tabControl1.SelectedIndex = 2;
xmlView.Navigate(txtURI.Text);
}
}
}
/*
* XmlTextReader reader = new XmlTextReader(@"c:\test2\1.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.AppendText("<");
this.richTextBox1.SelectionColor = Color.Brown;
this.richTextBox1.AppendText(reader.Name);
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.AppendText(">");
break;
case XmlNodeType.Text: //Display the text in each element.
this.richTextBox1.SelectionColor = Color.Black;
this.richTextBox1.AppendText(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.AppendText("</");
this.richTextBox1.SelectionColor = Color.Brown;
this.richTextBox1.AppendText(reader.Name);
this.richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.AppendText(">");
this.richTextBox1.AppendText("\n");
break;
}
}
*
*
*/