網站首頁 編程語言 正文
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");
}
原文鏈接:https://nostalgia.blog.csdn.net/article/details/108868793
相關推薦
- 2022-11-24 Linux使用iptables實現屏蔽ip地址的示例詳解_linux shell
- 2022-05-25 Tomcat配置JMX遠程連接的詳細操作_Tomcat
- 2023-04-01 Android自定義View?Material?Design理念詳解_Android
- 2022-07-19 Linux應該怎么使用命令
- 2022-11-11 Python?第三方庫?Pandas?數據分析教程_python
- 2022-07-19 C語言實現二叉樹的三種遍歷
- 2022-07-14 python利用線程生成不同尺寸的縮略圖實例詳解_python
- 2022-09-17 C++實現棧的操作(push和pop)_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支