C#后臺調用WebApi接口的實現(xiàn)方法
更新時間:2022年06月14日 10:05:51 作者:櫻花花
本文主要介紹了C#后臺調用WebApi接口的實現(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)建一個處理序列化的DataContractJsonSerializer
? ? ?DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
? ? ?MemoryStream ms = new MemoryStream();
? ? ?//將資料寫入MemoryStream
? ? ?serializer.WriteObject(ms, new People() { Id = 1, Name = "Hello ni" });
? ? ?//一定要在這設定Position
? ? ?ms.Position = 0;
? ? ?HttpContent content = new StreamContent(ms);//將MemoryStream轉成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");
}
到此這篇關于C#后臺調用WebApi接口的實現(xiàn)方法的文章就介紹到這了,更多相關C#后臺調用WebApi接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#中winform窗體實現(xiàn)注冊/登錄功能實例(DBHelper類)
在編寫項目時,編寫了一部分關于登錄頁面的一些代碼,下面這篇文章主要給大家介紹了關于C#中winform窗體實現(xiàn)注冊/登錄功能(DBHelper類)的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-06-06
淺談C#跨線程調用窗體控件(比如TextBox)引發(fā)的線程安全問題
下面小編就為大家分享一篇淺談C#跨線程調用窗體控件(比如TextBox)引發(fā)的線程安全問題,具有很好的參考價值,希望對大家有所幫助2017-11-11
Jquery+Ajax+Json+存儲過程實現(xiàn)高效分頁
這篇文章主要介紹Jquery+Ajax+Json+存儲過程實現(xiàn)分頁,需要的朋友可以參考下2015-08-08
C#通過第三方組件生成二維碼(QR Code)和條形碼(Bar Code)
用C#如何生成二維碼,我們可以通過現(xiàn)有的第三方dll直接來實現(xiàn),下面列出幾種不同的生成方法2016-12-12

