午夜视频在线观看区二区-午夜视频在线观看视频-午夜视频在线观看视频在线观看-午夜视频在线观看完整高清在线-午夜视频在线观看网站-午夜视频在线观看亚洲天堂

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C# HttpClient四種常用請求數據格式

admin
2025年2月15日 0:44 本文熱度 291

C#使用HttpClient四種請求數據格式:json、表單數據、文件上傳、xml。現在流行前后端分離,后端提供對應服務接口給前端或跨應用程序調用,如WebAPI等。在調用這些服務接口發送HTTP請求,而.NET為我們提供了HttpWebRequest、HttpClient幾個類庫來實現。

一、JSON數據格式

application/json

引用

using Newtonsoft.Json;using System;using System.Net.Http;using System.Net.Http.Headers;using System.Text;using System.Threading.Tasks;namespace Fountain.WinConsole.HttpDemo

代碼

static async Task Main(string[] args){	try	{		using (HttpClient httpClient = new HttpClient())		{			User user = new User();			user.username = "ceshi";			user.password = "123456";			string jsonData = JsonConvert.SerializeObject(user);			// 發送請求數據包			StringContent content = new StringContent(jsonData, Encoding.UTF8);			// 設置HTTP 響應上的ContentType --application/json			content.Headers.ContentType = new MediaTypeHeaderValue("application/json");			// 請求訪問地址			string url = "https://192.168.20.20/api/user/login";			// 發出HTTP的Post請求			HttpResponseMessage response = await httpClient.PostAsync(url, content);			// 讀取返回結果			string responseContent = await response.Content.ReadAsStringAsync();			// 將字符轉對象			Result result = JsonConvert.DeserializeObject<Result>(responseContent);		}	}	catch (Exception exception)	{		Console.WriteLine(exception.Message);	}	Console.ReadLine();}

二、表單數據格式

application/x-www-form-urlencoded

引用

using Newtonsoft.Json;using Newtonsoft.Json.Linq;using System;using System.Collections;using System.Collections.Generic;using System.Net.Http;using System.Net.Http.Headers;using System.Text;using System.Threading.Tasks;

代碼

static async Task Main(string[] args){	try	{		using (HttpClient httpClient = new HttpClient())		{			Dictionary<string,string> user = new Dictionary<stringstring>			{"username""ceshi" },"password""123456" }			};			// 發送請求數據包			FormUrlEncodedContent content = new FormUrlEncodedContent(user);			// 請求訪問地址			string url = "https://192.168.20.20/api/user/login";			// 發出HTTP的Post請求			HttpResponseMessage response = await httpClient.PostAsync(url, content);			// 讀取返回結果			string responseContent = await response.Content.ReadAsStringAsync();			// 將字符轉對象			Result result = JsonConvert.DeserializeObject<Result>(responseContent);		}	}	catch (Exception exception)	{		Console.WriteLine(exception.Message);	}	Console.ReadLine();}

三、文件上傳格式

multipart/form-data

引用

using Newtonsoft.Json;using Newtonsoft.Json.Linq;using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Net.Http;using System.Net.Http.Headers;using System.Text;using System.Threading.Tasks;

代碼

static async Task Main(string[] args){	try	{		using (HttpClient httpClient = new HttpClient())		{			MultipartFormDataContent multipartContent = new MultipartFormDataContent();			multipartContent.Add(new StringContent("user"), "test");			multipartContent.Add(new ByteArrayContent(File.ReadAllBytes(string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "test.jpg"))), "image", "test.jpg");			// 請求訪問地址			string url = "https://192.168.20.20/api/user/upload";			// 發出HTTP的Post請求			HttpResponseMessage response = await httpClient.PostAsync(url, multipartContent);			// 讀取返回結果			string responseContent = await response.Content.ReadAsStringAsync();			// 將字符轉對象			Result result = JsonConvert.DeserializeObject<Result>(responseContent);		}	}	catch (Exception exception)	{		Console.WriteLine(exception.Message);	}	Console.ReadLine();}}

四、XML數據格式

text/xml 

引用

using Newtonsoft.Json;using Newtonsoft.Json.Linq;using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Net.Http;using System.Net.Http.Headers;using System.Text;using System.Threading.Tasks;

代碼

static async Task Main(string[] args){	try	{		using (HttpClient httpClient = new HttpClient())		{			StringBuilder user = new StringBuilder();			user.AppendLine("<usrname>ceshi</usrname>");			user.AppendLine("<password>123456</password>");			string xmlData = user.ToString();			// 發送請求數據包			StringContent content = new StringContent(xmlData, Encoding.UTF8);			// 設置HTTP 響應上的ContentType --text/xml			content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");			// 請求訪問地址			string url = "https://192.168.20.20/api/user/login";			// 發出HTTP的Post請求			HttpResponseMessage response = await httpClient.PostAsync(url, content);			// 讀取返回結果			string responseContent = await response.Content.ReadAsStringAsync();			// 將字符轉對象			Result result = JsonConvert.DeserializeObject<Result>(responseContent);		}	}	catch (Exception exception)	{		Console.WriteLine(exception.Message);	}	Console.ReadLine();}

HttpClient的一些屬性與方法。

屬性:

BaseAddress

獲取或設置發送請求時地址。

DefaultProxy

獲取或設置全局HTTP請求代理。

DefaultRequestHeaders

獲取請求發送的標題。

DefaultRequestVersion

獲取或設置請求使用的默認HTTP版本。

MaxResponseContentBufferSize

獲取或設置讀取響應內容時要緩沖的最大字節數。

Timeout

獲取或設置請求超時等待的時間。

方法:

GetAsync

異步請求獲取指定URI。

GetByteArrayAsync

異步請求獲取指定URI并以字節數組的形式返回響應。

GetStreamAsync

異步請求獲取指定URI并以流的形式返回響應。

GetStringAsync

異步請求獲取指定URI并以字符串的形式返回響應正文。

PostAsync

異步將POST請求發送給指定URI。

Send

發送帶有指定請求的 HTTP 請求。

SendAsync

以異步操作發送 HTTP 請求。


閱讀原文:原文鏈接


該文章在 2025/2/17 12:23:54 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产精品色拉拉 | 国产午夜精品久久久久免费视 | 高清一区二区三区视频 | 国产性爱直播在线观看 | 国产成人精品免费视频大全麻豆 | 极品少妇高潮一区二区三区99 | 国产成人av免观看 | 成人日韩无码动漫秘一区二区 | 国产v一区二区久久久 | 国产不卡视频播放二区 | 国产在线不卡顿免费视频 | 国产白浆喷水在线视频免费看 | 国产麻豆剧传媒精品国产v精品 | 国产精品日韩亚洲一区二区 | 国产成人亚洲精品影院 | 91精品国产一区二区无码 | 成人女人a毛片在线看 | 国产精品猎奇系列在线观看 | 国产精品久久久久久久久久影院 | 国产亚洲高质量一路线二路线 | 精品无码国产自产在线观看 | 国产精品盗摄一区二区在线 | 国产精品免费av一区二区 | 国精产品一区二区三区公司 | 国产脚交足免 | 国产成a人片 | 狠狠爱无码一区二区三区 | 国产麻豆一级在线观看 | 国产一区二区丝袜美腿在线 | 成人年无码av片在线观看 | 国产精品亚洲专区无码影院 | 2025无码va在 | 精品丝袜国产自在线拍高清 | 国产va欧美va在线观看 | 精品国产一区二区三区免费91 | av网站免费在线观看精品 | 99久久国产宗和精品1上 | 国产极品美女到高潮无套久久 | 成人免费大片黄在线观看com | 国产av无码不卡 | 国产午夜成人无码免费看不卡 |