栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在C#中进行cURL调用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在C#中进行cURL调用

好吧,您不会直接调用cURL,而是使用以下选项之一:

  • HttpWebRequest
    /
    HttpWebResponse
  • WebClient
  • HttpClient
    (可从.NET 4.5开始)

我强烈建议使用

HttpClient
该类,因为从可用性的角度出发,该类的设计要比前两者更好。

对于您的情况,您可以这样做:

using System.Net.Http;var client = new HttpClient();// Create the HttpContent for the form to be posted.var requestContent = new FormUrlEnpredContent(new [] {    new KeyValuePair<string, string>("text", "This is a block of text"),});// Get the response.HttpResponseMessage response = await client.PostAsync(    "http://api.repustate.com/v2/demokey/score.json",    requestContent);// Get the response content.HttpContent responseContent = response.Content;// Get the stream of the content.using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())){    // Write the output.    Console.WriteLine(await reader.ReadToEndAsync());}

还应注意,

HttpClient
与前面提到的选项相比,该类对处理不同的响应类型有更好的支持,并且对异步操作(以及取消了它们)有更好的支持。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号