C# 調(diào)用WebApi的實現(xiàn)
更新時間:2021年04月19日 10:03:43 作者:櫻花花
這篇文章主要介紹了C# 調(diào)用WebApi的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
1.WebRequest方式
Post:
private void button1_Click(object sender, EventArgs e) { string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:\"test089\",Name:\"test1\"}"); } public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }
Get:
private void button1_Click(object sender, EventArgs e) { string ss = HttpGet("http://localhost:41558/api/Demo/GetXXX?Name=北京"); } public static string HttpGet(string url) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }
2.HttpClient 方式
Post:
private async void button2_Click(object sender, EventArgs e) { HttpClient client = new HttpClient(); //由HttpClient發(fā)出Delete Method HttpResponseMessage response = await client.DeleteAsync("http://localhost:41558/api/Demo"+"/1"); if (response.IsSuccessStatusCode) MessageBox.Show("成功"); } private async void button3_Click(object sender, EventArgs e) { //創(chuàng)建一個處理序列化的DataContractJsonSerializer DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People)); MemoryStream ms = new MemoryStream(); //將資料寫入MemoryStream serializer.WriteObject(ms, new People() { Id = 1, Name = "Hello ni" }); //一定要在這設(shè)定Position ms.Position = 0; HttpContent content = new StreamContent(ms);//將MemoryStream轉(zhuǎn)成HttpContent content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); HttpClient client = new HttpClient(); //由HttpClient發(fā)出Put Method HttpResponseMessage response = await client.PutAsync("http://localhost:41558/api/Demo"+ "/1", content); if (response.IsSuccessStatusCode) MessageBox.Show("成功"); }
Get:
using (WebClient client = new WebClient()) { client.Headers["Type"] = "GET"; client.Headers["Accept"] = "application/json"; client.Encoding = Encoding.UTF8; client.DownloadStringCompleted += (senderobj, es) => { var obj = es.Result; }; client.DownloadStringAsync("http://localhost:41558/api/Demo"); }
到此這篇關(guān)于C# 調(diào)用WebApi的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# 調(diào)用WebApi內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用Automation實現(xiàn)控制自動撥打接聽電話
這篇文章主要為大家詳細介紹了C#如何使用Automation實現(xiàn)控制自動撥打接聽電話,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C#復(fù)雜XML反序列化為實體對象兩種方式小結(jié)
本文主要介紹了C#復(fù)雜XML反序列化為實體對象兩種方式,主要介紹如何把通過接口獲取到的Xml數(shù)據(jù)轉(zhuǎn)換成(反序列化)我們想要的實體對象,感興趣的可以一起來了解一下2022-04-04C#使用SqlDataAdapter對象獲取數(shù)據(jù)的方法
這篇文章主要介紹了C#使用SqlDataAdapter對象獲取數(shù)據(jù)的方法,結(jié)合實例形式較為詳細的分析了SqlDataAdapter對象獲取數(shù)據(jù)具體步驟與相關(guān)使用技巧,需要的朋友可以參考下2016-02-02C#學(xué)習(xí)筆記- 隨機函數(shù)Random()的用法詳解
下面小編就為大家?guī)硪黄狢#學(xué)習(xí)筆記- 隨機函數(shù)Random()的用法詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08