您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

2026-03-31 09:501阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计468个文字,预计阅读时间需要2分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

目录

1.WebRequest 方式

2.HttpClient 方式

1. WebRequest 方式

csharpprivate void button1_Click(object sender, EventArgs e){ string ss=HttpPost(http://localhost:41558/api/Demo/PostXXX, {\Code\:\test089\,\Name\:\te\});}

2. HttpClient 方式csharpprivate void button1_Click(object sender, EventArgs e){ string ss=HttpPost(http://localhost:41558/api/Demo/PostXXX, {\Code\:\test089\,\Name\:\te\});}

目录
  • 1.WebRequest方式
  • 2.HttpClient 方式

1.WebRequest方式

private void button1_Click(object sender, EventArgs e)         {            string ss= HttpPost("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("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("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("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("localhost:41558/api/Demo"); }

到此这篇关于C#后台调用WebApi接口的实现方法的文章就介绍到这了,更多相关C#后台调用WebApi接口内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

本文共计468个文字,预计阅读时间需要2分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

目录

1.WebRequest 方式

2.HttpClient 方式

1. WebRequest 方式

csharpprivate void button1_Click(object sender, EventArgs e){ string ss=HttpPost(http://localhost:41558/api/Demo/PostXXX, {\Code\:\test089\,\Name\:\te\});}

2. HttpClient 方式csharpprivate void button1_Click(object sender, EventArgs e){ string ss=HttpPost(http://localhost:41558/api/Demo/PostXXX, {\Code\:\test089\,\Name\:\te\});}

目录
  • 1.WebRequest方式
  • 2.HttpClient 方式

1.WebRequest方式

private void button1_Click(object sender, EventArgs e)         {            string ss= HttpPost("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("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("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("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("localhost:41558/api/Demo"); }

到此这篇关于C#后台调用WebApi接口的实现方法的文章就介绍到这了,更多相关C#后台调用WebApi接口内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。