如何在我的程序中实现curl(Pushbullet API)?

时间:2016-03-23 13:02:25

标签: c# php api curl pushbullet

如何将官方Pushbullet API网站上提供的curl命令实现到我的C#程序中?

示例:

curl --header 'Access-Token: <your_access_token_here>' \
https://api.pushbullet.com/v2/users/me

我可以以某种方式直接将该代码写入我的C#程序或者我是否使用过PHP?

1 个答案:

答案 0 :(得分:0)

您可以使用HttpClient

以下是使用httpClient

进行get / post请求的简单代码段
public async Task<T> MakeHttpClientRequestASync<T>(string requestUrl, string authenticationToken,
                Dictionary<string, string> requestContent, HttpMethod verb, Action<Exception> error)
{
     var httpClient = new HttpClient();
    //// add access token to AuthenticationHeader
     httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Access-Token",authenticationToken);
     HttpResponseMessage response;

     var returnVal = default(T);

     try
      {
       if (verb == HttpMethod.Post)
        {
         response = await httpClient.PostAsync(requestUrl, new FormUrlEncodedContent(requestContent));
        }
        else
        {
           response = await httpClient.GetAsync(requestUrl);
        }

        var resultString = await response.Content.ReadAsStringAsync();
        //// DeserializeObject using Json.net
        returnVal = JsonConvert.DeserializeObject<T>(resultString);
       }
       catch (Exception ex)
       {
         error(ex);
       }

    return returnVal;
}

并以这种方式称呼它:

MakeHttpClientRequestASync<T>("https://api.pushbullet.com/v2/users/me","your auth token",null, HttpMethod.Get,(errorAction)=>
                        {
                            // do something}
                        });