如何在我的程序中实现 curl (Pushbullet API)?
How can I implement curl (Pushbullet API) into my program?
如何在我的 C# 程序中实现官方 Pushbullet API 站点上给出的 curl 命令?
示例:
curl --header 'Access-Token: <your_access_token_here>' \
https://api.pushbullet.com/v2/users/me
我能否以某种方式直接将该代码写入我的 C# 程序,或者我必须使用 php?
你可以使用 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}
});
如何在我的 C# 程序中实现官方 Pushbullet API 站点上给出的 curl 命令?
示例:
curl --header 'Access-Token: <your_access_token_here>' \
https://api.pushbullet.com/v2/users/me
我能否以某种方式直接将该代码写入我的 C# 程序,或者我必须使用 php?
你可以使用 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}
});