1-) C# Athena - web service yazımı rest ile -> Client -> c# aplication
web service yazımı rest ile -> Client -> c# aplication
1-) Refarances -> Nuget -> Microsoft.AspNet.WebApi.Client -> install
2-) person.cs Sınıfı aşağıda
class person {
public int id { get; set; }
public string ad { get; set; }
public string soyad { get; set; }
}
3-) Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestWebServiceClient.Model;
namespace RestWebServiceClient {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Task T = new Task(RunAsyncGet);
T.Start();
Task T2 = new Task(RunAsyncPOST);
T2.Start();
Task T3 = new Task(RunAsyncPUT);
T3.Start();
Task T4 = new Task(RunAsyncDELETE);
T4.Start();
}
// http://localhost:60308/api/person/5
async void RunAsyncGet() { // çağırmak
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("http://localhost:60308/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("aplication/json"));
HttpResponseMessage response = await client.GetAsync("api/person/5");
if (response.IsSuccessStatusCode) {
person person = await response.Content.ReadAsAsync<person>();
}
}
}
async void RunAsyncPOST() { // eklemek
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("http://localhost:60308/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("aplication/json"));
person p = new person();
p.id = 2;
p.ad = "RECEP";
p.soyad = "EMEÇ";
HttpResponseMessage response = await client.PostAsJsonAsync("api/person/3", p);
if (response.IsSuccessStatusCode) {
Uri personURL = response.Headers.Location;
Console.WriteLine(personURL);
}
}
}
async void RunAsyncPUT() { // güncellemek
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("http://localhost:60308/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("aplication/json"));
person p = new person();
p.id = 2;
p.ad = "RECEP22";
p.soyad = "EMEÇ";
HttpResponseMessage response = await client.PutAsJsonAsync("api/person/3", p);
if (response.IsSuccessStatusCode) {
Uri personURL = response.Headers.Location;
Console.WriteLine(personURL);
}
}
}
async void RunAsyncDELETE() { // silmek
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("http://localhost:60308/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("aplication/json"));
person p = new person();
p.id = 2;
p.ad = "RECEP22";
p.soyad = "EMEÇ";
HttpResponseMessage response = await client.DeleteAsync("api/person/13");
if (response.IsSuccessStatusCode) {
Uri personURL = response.Headers.Location;
Console.WriteLine(personURL);
}
}
}
}
}