C# 調(diào)用WebApi的實(shí)現(xiàn)
更新時(shí)間:2021年04月19日 10:03:43 作者:櫻花花
這篇文章主要介紹了C# 調(diào)用WebApi的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(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)建一個(gè)處理序列化的DataContractJsonSerializer
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
MemoryStream ms = new MemoryStream();
//將資料寫(xiě)入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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# GetMethod方法的應(yīng)用實(shí)例講解
GetMethod 是獲取當(dāng)前 Type 的特定方法,具有多個(gè)重載, GetMethod 即使用指定的綁定約束搜索指定方法,本文給大家介紹了C# GetMethod方法的應(yīng)用實(shí)例,需要的朋友可以參考下2024-04-04
C#編程簡(jiǎn)單實(shí)現(xiàn)生成PDF文檔的方法示例
這篇文章主要介紹了C#編程簡(jiǎn)單實(shí)現(xiàn)生成PDF文檔的方法,結(jié)合實(shí)例形式分析了C#生成PDF文檔的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
C#字典Dictionary的用法說(shuō)明(注重性能版)
這篇文章主要介紹了C#字典Dictionary的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C#延時(shí)關(guān)閉電腦、取消關(guān)閉電腦操作方法(需管理員權(quán)限)
在C#中,如果想實(shí)現(xiàn)延時(shí)關(guān)閉電腦和取消關(guān)閉的功能,可以有多種方法,下面給大家分享C#延時(shí)關(guān)閉電腦、取消關(guān)閉電腦操作方法,感興趣的朋友一起看看吧2024-06-06
C# 正則判斷一個(gè)數(shù)字的格式是否有逗號(hào)的代碼
c#正則判斷一個(gè)格式化數(shù)字里是否有逗號(hào)的解決方法2008-07-07

