精品秘无码一区二区三区老师-精品秘一区二三区免费雷安-精品蜜桃秘一区二区三区-精品蜜桃秘一区二区三区粉嫩-精品蜜桃一区二区三区-精品蜜臀国产aⅴ一区二区三区

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

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

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

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

主站蜘蛛池模板: 国产精品自在线天天看 | 亚洲中文字幕网站在线播放 | 亚洲另类激情综合偷自拍图 | 性中国熟女毛耸耸性视频 | 亚洲经典千人经典日产 | 国产好大好爽久久久久久久 | 亚洲一区在线观看视频 | 成人国产网站v片免费观看 成人国产日本亚洲精品 | 国产盗摄在线一区二区 | 亚洲伊人成综合成人网 | 日本三线和韩国三线品牌对比 | 日产精品久久久久久久 | 久久久久黑人强伦姧人妻 | 中出人妻中文字幕91 | 丝袜制服中文字幕 | 91狠狠色丁香婷婷综合久久 | 51久久国产露脸精品国产 | 色偷偷超碰av男人天堂 | 自拍视频一区二区三区果冻 | 亚洲av国产av综合av | 五月丁香合缴情在线看 | 韩国日本三级在线观看 | 麻豆久久婷婷综合五月国产 | 亚洲av日韩av永久无码下载 | 亚洲国产精品一区二区久久 | 午夜福利短视频欧美 | 国产av成人一区二区三区 | 国产日韩精品一区二区在线观看 | 无码人妻精品一区二区三区A片 | 三级国产免费影视 | 精品免费国产一区二区三区 | 亚洲成aⅴ人片在线观看 | 另类综合欧美中文字幕 | 色欲狠狠躁天天躁无码中文字幕 | 97天天碰夜夜操 | 2025亚洲卡一卡二新区入口 | 久久久国产精品福利免费 | 国产精品拍天天在线 | 欧美日韩激情无码专区 | 东京热久久精品 | 日夜精品综合网,7799 |