asp.net core HttpClient post ,get

浪淘沙5个月前后端112

1、post

using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

var url = "https://api.dingtalk.com/v1.0/oauth2/userAccessToken";

var body = new
{
    clientId = AppKey,
    clientSecret = AppSecret,
    code = code,
    grantType = "authorization_code"
};

var json = JsonSerializer.Serialize(body);

using var client = new HttpClient();

// Bearer Token,如果有需要
client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", accessToken);

var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync(url, content);

var result = await response.Content.ReadAsStringAsync();

using var doc = JsonDocument.Parse(result);

string token = doc.RootElement.GetProperty("accessToken").GetString();


2、get

using var client = new HttpClient();
// 设置请求头 Authorization
client.DefaultRequestHeaders.Add("x-acs-dingtalk-access-token", accessToken);
// 发起 GET 请求
var response = await client.GetAsync("https://api.dingtalk.com/v1.0/contact/users/me");
response.EnsureSuccessStatusCode();
// 读取响应
var json = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);


相关文章

asp.net core furion 前端传入json,后端api接入并角析

{   "result": {     "cover": {  ...

asp.net core post

<form class="layui-form layui-form-pane" action="" method...

wangeditor编辑器

<!--编辑器--> <link href="/wangeditor/style.css" rel="stylesheet&quo...

asp.net core HttpClient 返回值

  public async Task<IActionResult> OnPostDown()   {  ...

asp.net core razor加载模板文件路径

  var templateContent = "${include(\"" + App.HostE...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。