Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using static System.Windows.Forms.ListViewItem;
- namespace Client
- {
- /**
- * Progetto: creare un client che permetta l'esecuzione dei metodi HTTP CRUD (Get, Post, Put, Delete) su una web API utilizzando metodi Asincroni
- * e premettendo l'autenticazione e con una piccola gestione degli errori
- * L'app si può provare sui siti https://jsonplaceholder.typicode.com/ e https://dummy.restapiexample.com/
- */
- public partial class Form1 : Form
- {
- private ICredentials credentials = new NetworkCredential();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private async void Get(string url)
- {
- using (var client = new WebClient())
- {
- client.Credentials = credentials;
- foreach (ListViewItem item in lstQuery.Items)
- {
- client.QueryString.Add(item.SubItems[0].Text, item.SubItems[1].Text);
- }
- try
- {
- var result = await client.DownloadStringTaskAsync(new Uri(url));
- txtOutput.Text = result;
- }
- catch (WebException e)
- {
- HandleException(e);
- }
- }
- }
- private async void ExecuteMethod(string method, string url)
- {
- var values = new NameValueCollection();
- using (var client = new WebClient())
- {
- client.Credentials = credentials;
- foreach (ListViewItem item in lstQuery.Items)
- {
- values.Add(item.SubItems[0].Text, item.SubItems[1].Text);
- }
- try
- {
- var response = await client.UploadValuesTaskAsync(url, method, values);
- txtOutput.Text = Encoding.ASCII.GetString(response);
- }
- catch (WebException e)
- {
- HandleException(e);
- }
- }
- }
- private void HandleException(WebException exception)
- {
- if (exception.Status == WebExceptionStatus.ProtocolError)
- {
- MessageBox.Show("Errore, status code: " + ((HttpWebResponse)exception.Response).StatusCode);
- }
- else if (exception.Status == WebExceptionStatus.Timeout)
- MessageBox.Show("Errore: timeout");
- else if (exception.Status == WebExceptionStatus.ConnectFailure)
- MessageBox.Show("Connessione fallita");
- else
- MessageBox.Show("Errore: " + exception.ToString());
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- var key = txtKey.Text;
- var value = txtValue.Text;
- lstQuery.Items.Add(new ListViewItem(new string[] { key, value }));
- }
- private void btnGet_Click(object sender, EventArgs e)
- {
- Get(txtUrl.Text);
- }
- private void btnPost_Click(object sender, EventArgs e)
- {
- ExecuteMethod("POST", txtUrl.Text);
- }
- private void btnPut_Click(object sender, EventArgs e)
- {
- ExecuteMethod("PUT", txtUrl.Text);
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- ExecuteMethod("DELETE", txtUrl.Text);
- }
- private void btnClear_Click(object sender, EventArgs e)
- {
- txtOutput.Text = "";
- }
- private void btnGetSync_Click(object sender, EventArgs e)
- {
- using (var client = new WebClient())
- {
- foreach (ListViewItem item in lstQuery.Items)
- {
- client.QueryString.Add(item.SubItems[0].Text, item.SubItems[1].Text);
- }
- try
- {
- var result = client.DownloadString(txtUrl.Text);
- txtOutput.Text = result;
- }
- catch (WebException exc)
- {
- HandleException(exc);
- }
- }
- }
- private void btnSet_Click(object sender, EventArgs e)
- {
- this.credentials = new NetworkCredential(txtUsername.Text, txtPassword.Text);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement