本文主要介绍了C#后台调用WebApi接口的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
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发出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)
{
//创建一个处理序列化的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发出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接口的实现方法的文章就介绍到这了,更多相关C#后台调用WebApi接口内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
织梦狗教程
本文标题为:C#后台调用WebApi接口的实现方法


基础教程推荐
猜你喜欢
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C# 解析XML和反序列化的示例 2023-04-14
- C#中的Linq to JSON操作详解 2023-06-08
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity shader实现高斯模糊效果 2023-01-16