C# 調(diào)用WebApi的實(shí)現(xiàn)
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)建一個(gè)處理序列化的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的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# 調(diào)用WebApi內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何在C#項(xiàng)目中鏈接一個(gè)文件夾下的所有文件詳解
很多時(shí)候我們需要獲取一個(gè)結(jié)構(gòu)未知的文件夾下所有的文件或是指定類型的所有文件,下面這篇文章主要給大家介紹了關(guān)于如何在C#項(xiàng)目中鏈接一個(gè)文件夾下的所有文件,需要的朋友可以參考下2023-02-02Unity技術(shù)手冊之Button按鈕使用實(shí)例詳解
這篇文章主要為大家介紹了Unity技術(shù)手冊之Button按鈕使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11C#使用Automation實(shí)現(xiàn)控制自動撥打接聽電話
這篇文章主要為大家詳細(xì)介紹了C#如何使用Automation實(shí)現(xiàn)控制自動撥打接聽電話,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C#復(fù)雜XML反序列化為實(shí)體對象兩種方式小結(jié)
本文主要介紹了C#復(fù)雜XML反序列化為實(shí)體對象兩種方式,主要介紹如何把通過接口獲取到的Xml數(shù)據(jù)轉(zhuǎn)換成(反序列化)我們想要的實(shí)體對象,感興趣的可以一起來了解一下2022-04-04C#使用SqlDataAdapter對象獲取數(shù)據(jù)的方法
這篇文章主要介紹了C#使用SqlDataAdapter對象獲取數(shù)據(jù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了SqlDataAdapter對象獲取數(shù)據(jù)具體步驟與相關(guān)使用技巧,需要的朋友可以參考下2016-02-02C#省份城市下拉框聯(lián)動簡單實(shí)現(xiàn)方法
這篇文章主要介紹了C#省份城市下拉框聯(lián)動簡單實(shí)現(xiàn)方法,涉及字典的定義與索引的用法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx
一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04C#學(xué)習(xí)筆記- 隨機(jī)函數(shù)Random()的用法詳解
下面小編就為大家?guī)硪黄狢#學(xué)習(xí)筆記- 隨機(jī)函數(shù)Random()的用法詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08