将curl转换为c# [英] convertion of curl to c#
本文介绍了将curl转换为c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
curl https://api.smartsheet.com/1.1/sheets \-H授权:Bearer ACCESS_TOKEN将其转换为C#。我必须提供网址请求
curl https://api.smartsheet.com/1.1/sheets \-H "Authorization: Bearer ACCESS_TOKEN" convert it into C# .the url i have to give to the web request
推荐答案
好吧,你不会直接调用cURL,而是使用以下选项之一:
HttpWebRequest / HttpWebResponse
WebClient
HttpClient(可从.NET 4.5开始)
我强烈建议使用HttpClient类,因为它'设计得比前两个好得多(从可用性的角度来看)。
在你的情况下,你会这样做:
使用System.Net.Http;
var client = new HttpClient();
//为要发布的表单创建HttpContent。
var requestContent = new FormUrlEncodedContent(new [] {
new KeyValuePair< string,> ;(文字,这是一段文字),
});
//获取回复。
HttpResponseMessage response = await client.PostAsync(
http://api.repustate.com/v2/demokey/score .json,
requestContent);
//获取响应内容。
HttpContent responseContent = response .Content;
//获取内容流。
using(var reader = new StreamReader(await responseContent.ReadAsStreamAsync() ))
{
//写输出。
Console.WriteLine(等待reader.ReadToEndAsync());
}
Well, you wouldn''t call cURL directly, rather, you''d use one of the following options:
HttpWebRequest/HttpWebResponse
WebClient
HttpClient (available from .NET 4.5 on)
I''d highly recommend using the HttpClient class, as it''s engineered to be much better (from a usability standpoint) than the former two.
In your case, you would do this:
using System.Net.Http;
var client = new HttpClient();
// Create the HttpContent for the form to be posted.
var requestContent = new FormUrlEncodedContent(new [] {
new KeyValuePair<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());
}
这篇关于将curl转换为c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文