如何在C#程序中实现官方Pushbullet API站点上给出的curl命令?

例:

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


我可以以某种方式直接将该代码编写到我的C#程序中,还是可以使用php?

最佳答案

您可以使用HttpClient

这是我使用httpClient进行获取/发布请求的简单代码段

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}
                        });

10-06 16:16