C#后臺調(diào)用WebApi接口的實(shí)現(xiàn)方法
1.WebRequest方式
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#中winform窗體實(shí)現(xiàn)注冊/登錄功能實(shí)例(DBHelper類)
在編寫項(xiàng)目時(shí),編寫了一部分關(guān)于登錄頁面的一些代碼,下面這篇文章主要給大家介紹了關(guān)于C#中winform窗體實(shí)現(xiàn)注冊/登錄功能(DBHelper類)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題
下面小編就為大家分享一篇淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題,具有很好的參考價(jià)值,希望對大家有所幫助2017-11-11WinForm實(shí)現(xiàn)窗體最大化并遮蓋任務(wù)欄的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)窗體最大化并遮蓋任務(wù)欄的方法,涉及C#實(shí)現(xiàn)WinForm窗體全屏顯示的實(shí)現(xiàn)及調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08Jquery+Ajax+Json+存儲過程實(shí)現(xiàn)高效分頁
這篇文章主要介紹Jquery+Ajax+Json+存儲過程實(shí)現(xiàn)分頁,需要的朋友可以參考下2015-08-08C#通過第三方組件生成二維碼(QR Code)和條形碼(Bar Code)
用C#如何生成二維碼,我們可以通過現(xiàn)有的第三方dll直接來實(shí)現(xiàn),下面列出幾種不同的生成方法2016-12-12