公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

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

C#調用WebApi請求常用的兩種方式

admin
2025年1月2日 11:29 本文熱度 3571

1.WebRequest方式

引用dll 

using System.IO;using System.Net;using System.Threading.Tasks;
//Postpublic static string HttpPost(string url, string body){	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(); }}
//GETpublic static string HttpGet(string url){ 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 方式

static HttpClient client = new HttpClient();
//根據 ID 獲取產品static async Task<Uri> CreateProductAsync(Product product){ HttpResponseMessage response = await client.PostAsJsonAsync( "api/products", product); response.EnsureSuccessStatusCode();
// return URI of the created resource. return response.Headers.Location;}
//創建新產品static async Task<Product> GetProductAsync(string path){ Product product = null; HttpResponseMessage response = await client.GetAsync(path); if (response.IsSuccessStatusCode) { product = await response.Content.ReadAsAsync<Product>(); } return product;}
//更新產品static async Task<Product> UpdateProductAsync(Product product){ HttpResponseMessage response = await client.PutAsJsonAsync( $"api/products/{product.Id}", product); response.EnsureSuccessStatusCode();
// Deserialize the updated product from the response body. product = await response.Content.ReadAsAsync<Product>(); return product;}
//刪除產品static async Task<HttpStatusCode> DeleteProductAsync(string id){ HttpResponseMessage response = await client.DeleteAsync( $"api/products/{id}"); return response.StatusCode;}
HttpClient 異步調用
static async Task RunAsync(){    client.BaseAddress = new Uri("http://localhost:64195/");    client.DefaultRequestHeaders.Accept.Clear();    client.DefaultRequestHeaders.Accept.Add(        new MediaTypeWithQualityHeaderValue("application/json"));
    try    {        Product product = new Product        {            Name = "Gizmo",            Price = 100,            Category = "Widgets"        };
        var url = await CreateProductAsync(product);        Console.WriteLine($"Created at {url}");
        // Get the product        product = await GetProductAsync(url.PathAndQuery);        ShowProduct(product);
        // Update the product        Console.WriteLine("Updating price...");        product.Price = 80;        await UpdateProductAsync(product);
        // Get the updated product        product = await GetProductAsync(url.PathAndQuery);        ShowProduct(product);
        // Delete the product        var statusCode = await DeleteProductAsync(product.Id);        Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");
    }    catch (Exception e)    {        Console.WriteLine(e.Message);    }
    Console.ReadLine();}


閱讀原文:原文鏈接


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

主站蜘蛛池模板: 国产91成人在在线播放 | 成人亚洲欧美成αⅴ人在线观看 | 国产精品亚洲综合免费观看 | av免费在线播放网址 | 99精品国产免费久久 | 精品国产一区二区三区久久狼黑人 | 丰满老熟女一级aa片色欲 | 国产极品粉嫩馒头一线天 | 国产亚洲专区在线观 | 2025国产午夜福利免费看 | 2025最新国产三级在线看 | 成人国内精品视频在线观看 | 国产精品高潮森林久久av无码 | 国产欧美日韩免费 | 国产高清无码在线一区二区 | 高潮国产精品一区二区 | 国产欧美日韩综合 | 国产无码精品a级在线 | 国产精品免费观看一区二区 | 91尤物无码国产在线观看 | 国产美女精品久久久久久久免费 | 妓女妓女影院妓女视频妓女影库 | 精品一区二区免费视频 | 不卡国产视频第一页 | 国产麻豆剧传媒精品国 | 国产产精品亚洲一区二区在线观看 | 国产精品萌白酱在线观看 | 国产无套视频免费看 | 69黄在线看片 | 91海角大神邻居在线观看 | 精品爆乳一区二区三区无码av | 国产99久久精品 | 精品国产尤物一区二区三区 | 精品妓女久久久久亚洲中文 | 国产精品日产欧美在线一区 | 国产午夜精品一区二区不卡 | 国产av无码日韩av无码网站 | 国产成人一区二区在线观看 | 国产91久久精品久久精品 | a级国产免费黄片 | 国产每日精品亚洲精品 |