HttpClient 对接json 参数,parameter参数 webapi

HttpClient 对接json 参数,parameter参数,header,post

huawei api :  https://support.huaweicloud.com/usermanual-lts/lts_04_0216.html

using Newtonsoft.Json;
using System.Text;
using ***.HuaweiLog.Model;

HttpClient 对接json 参数,header,post
public async void WriteLog(string bussinesType, string messageStr, string token)
{
try
{
//string url = "https://lts-access.cn-south-1.myhuaweicloud.com/v2/***/lts/groups/***/streams/***/tenant/contents";
//string tokenstr = "xyz“;
string url = _writeLogUrl;
string tokenstr = token;

logModel logInfo = new logModel();
long time = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();

logInfo.log_time_ns = time * 1000000000;// 纳秒
logInfo.contents = messageStr;
logInfo.labels.user_tag = bussinesType;

var json = JsonConvert.SerializeObject(logInfo);
var data = new StringContent(json, Encoding.UTF8, "application/json");

using (var httpClientHandler = new HttpClientHandler() { ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true })

using (var client = new HttpClient(httpClientHandler))
{
client.DefaultRequestHeaders.TryAddWithoutValidation("X-Auth-Token", tokenstr);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-type", "application/json;charset=UTF-8");

var response = await client.PostAsync(url, data);
//string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine($"time:"+ System.DateTime.Now.ToString("G") + ",log result:" + result);
}
}
catch (Exception ex)
{
Console.WriteLine($"华为云日志方法调用失败WriteLog(),msg:" + ex.Message);
}
}

HttpClient 对接parameter 参数,header,post
public void WriteLog(string logTag, string logMessage)
{
string url = "http://***/api/HuaweiLog/WriteLog"; //webapi url
Dictionary<string, string> Params = new Dictionary<string, string>();
Params.Add("groupId", "***"); // 华为日志组ID,在华为云LTS日志控制台华为日志组查找,读配置文件
Params.Add("streamId", "***"); // 华为日志组下面的日志流,在华为云LTS日志控制台华为日志组下面的日志流查找,读配置文件

Params.Add("logTag", logTag); // 日志标签
Params.Add("logMessage", logMessage); // 日志内容

HttpClient client = new HttpClient();
client.PostAsync(url, new FormUrlEncodedContent(Params));
}

public class logModel
{
public long log_time_ns { get; set; }
public string contents { get; set; }
public labels labels = new labels();
}

public class labels
{
public string user_tag { get; set; }

}

posted @ 2023-02-16 16:07  大树2  阅读(109)  评论(0编辑  收藏  举报