服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# 调用WebApi的实现

C# 调用WebApi的实现

2022-11-14 12:30樱花花 C#

这篇文章主要介绍了C# 调用WebApi的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.WebRequest方式

Post:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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:

?
1
2
3
4
5
6
7
8
9
10
11
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_37192571/article/details/108868793

延伸 · 阅读

精彩推荐
  • C#unity 文件流读取图片与www读取图片的区别介绍

    unity 文件流读取图片与www读取图片的区别介绍

    这篇文章主要介绍了unity 文件流读取图片与www读取图片的对比分析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    贪玩的孩纸时代8242022-11-12
  • C#简单聊聊c# 事件

    简单聊聊c# 事件

    这篇文章主要介绍了c# 事件的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    Learning hard8552022-09-28
  • C#Quartz.Net任务和触发器实现方法详解

    Quartz.Net任务和触发器实现方法详解

    这篇文章主要介绍了Quartz.Net任务和触发器实现方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可...

    David Huang4552022-10-19
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网8142021-12-21
  • C#Unity实现换装系统

    Unity实现换装系统

    这篇文章主要为大家详细介绍了Unity实现换装系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    无忧酒不加糖11802022-11-11
  • C#C#设置文件权限的方法

    C#设置文件权限的方法

    这篇文章主要介绍了C#设置文件权限的方法,文中讲解非常细致,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下...

    彭泽090210452022-09-29
  • C#C# HttpClient 如何使用 Consul 发现服务

    C# HttpClient 如何使用 Consul 发现服务

    这篇文章主要介绍了C# HttpClient 如何使用 Consul 发现服务,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    zhouandke6032022-11-01
  • C#c# 实现的支付宝支付

    c# 实现的支付宝支付

    这篇文章主要介绍了c# 实现的支付宝支付的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    海市蜃楼20207042022-10-26