· 6 years ago · Mar 30, 2020, 08:32 AM
1using System;
2using System.Collections.Generic;
3using System.Collections.Specialized;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.Linq;
8using System.Net;
9using System.Text;
10using System.Threading.Tasks;
11using System.Windows.Forms;
12using static System.Windows.Forms.ListViewItem;
13
14namespace Client
15{
16 /**
17 * Progetto: creare un client che permetta l'esecuzione dei metodi HTTP CRUD (Get, Post, Put, Delete) su una web API utilizzando metodi Asincroni
18 * e premettendo l'autenticazione e con una piccola gestione degli errori
19 * L'app si può provare sui siti https://jsonplaceholder.typicode.com/ e https://dummy.restapiexample.com/
20 */
21 public partial class Form1 : Form
22 {
23 private ICredentials credentials = new NetworkCredential();
24
25 public Form1()
26 {
27 InitializeComponent();
28 }
29
30 private void Form1_Load(object sender, EventArgs e)
31 {
32
33 }
34
35 private async void Get(string url)
36 {
37 using (var client = new WebClient())
38 {
39 client.Credentials = credentials;
40 foreach (ListViewItem item in lstQuery.Items)
41 {
42 client.QueryString.Add(item.SubItems[0].Text, item.SubItems[1].Text);
43 }
44 try
45 {
46 var result = await client.DownloadStringTaskAsync(new Uri(url));
47 txtOutput.Text = result;
48 }
49 catch (WebException e)
50 {
51 HandleException(e);
52 }
53 }
54 }
55
56 private async void ExecuteMethod(string method, string url)
57 {
58 var values = new NameValueCollection();
59 using (var client = new WebClient())
60 {
61 client.Credentials = credentials;
62 foreach (ListViewItem item in lstQuery.Items)
63 {
64 values.Add(item.SubItems[0].Text, item.SubItems[1].Text);
65 }
66 try
67 {
68 var response = await client.UploadValuesTaskAsync(url, method, values);
69 txtOutput.Text = Encoding.ASCII.GetString(response);
70 }
71 catch (WebException e)
72 {
73 HandleException(e);
74 }
75 }
76 }
77
78 private void HandleException(WebException exception)
79 {
80 if (exception.Status == WebExceptionStatus.ProtocolError)
81 {
82 MessageBox.Show("Errore, status code: " + ((HttpWebResponse)exception.Response).StatusCode);
83 }
84 else if (exception.Status == WebExceptionStatus.Timeout)
85 MessageBox.Show("Errore: timeout");
86 else if (exception.Status == WebExceptionStatus.ConnectFailure)
87 MessageBox.Show("Connessione fallita");
88 else
89 MessageBox.Show("Errore: " + exception.ToString());
90 }
91
92 private void btnAdd_Click(object sender, EventArgs e)
93 {
94 var key = txtKey.Text;
95 var value = txtValue.Text;
96 lstQuery.Items.Add(new ListViewItem(new string[] { key, value }));
97 }
98
99 private void btnGet_Click(object sender, EventArgs e)
100 {
101 Get(txtUrl.Text);
102 }
103
104 private void btnPost_Click(object sender, EventArgs e)
105 {
106 ExecuteMethod("POST", txtUrl.Text);
107 }
108
109 private void btnPut_Click(object sender, EventArgs e)
110 {
111 ExecuteMethod("PUT", txtUrl.Text);
112 }
113
114 private void btnDelete_Click(object sender, EventArgs e)
115 {
116 ExecuteMethod("DELETE", txtUrl.Text);
117 }
118
119 private void btnClear_Click(object sender, EventArgs e)
120 {
121 txtOutput.Text = "";
122 }
123
124 private void btnGetSync_Click(object sender, EventArgs e)
125 {
126 using (var client = new WebClient())
127 {
128 foreach (ListViewItem item in lstQuery.Items)
129 {
130 client.QueryString.Add(item.SubItems[0].Text, item.SubItems[1].Text);
131 }
132 try
133 {
134 var result = client.DownloadString(txtUrl.Text);
135 txtOutput.Text = result;
136 }
137 catch (WebException exc)
138 {
139 HandleException(exc);
140 }
141 }
142 }
143
144 private void btnSet_Click(object sender, EventArgs e)
145 {
146 this.credentials = new NetworkCredential(txtUsername.Text, txtPassword.Text);
147 }
148 }
149}