12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
-
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Net.Http;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Security.Cryptography;
- using System.Net.Http.Json;
- using System.Text.Json;
- using System.IO;
- using TEAMModelOS.SDK.Extension;
- using System.Net;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelOS.SDK.DI
- {
-
- public class HttpTrigger
- {
- private readonly HttpClient _httpClient;
- public HttpTrigger(HttpClient httpClient)
- {
- _httpClient = httpClient;
- }
- // <summary>
- /// 请求信息
- /// </summary>
- /// <param name="robotUrl">釘釘Robot發送Url</param>
- /// <param name="secret">加簽密鑰</param>
- /// <param name="msg">發送訊息</param>
- /// <returns></returns>
- public async Task<(int status, string json)> RequestHttpTrigger(object data, string location, string url)
- {
- var keys = HttpTriggerUrl.HttpTrigger地址.GetDescriptionText().Split(',');
- string domain = "";
- if (location.Equals("China-Dep"))
- {
- domain = keys[0];
- }
- else if (location.Equals("China-Test"))
- {
- domain = keys[1];
- }
- else if (location.Equals("China"))
- {
- domain = keys[2];
- }
- else if (location.Equals("Global-Dep"))
- {
- domain = keys[3];
- }
- else if (location.Equals("Global-Test"))
- {
- domain = keys[3];
- }
- else if (location.Equals("Global"))
- {
- domain = keys[4];
- }
- string link = domain.Contains("localhost") ? $"http://{domain}/api/{url}" : $"https://{domain}/api/{url}";
- HttpContent httpContent = new StringContent(data.ToJsonString());
- // _httpClient.Timeout= TimeSpan.FromSeconds(30);
- HttpResponseMessage responseMessage = await _httpClient.PostAsync(link, httpContent);
- if (responseMessage.StatusCode == HttpStatusCode.OK)
- {
- string Content = await responseMessage.Content.ReadAsStringAsync();
- if (string.IsNullOrWhiteSpace(Content))
- {
- return (200, null) ;
- }
- else {
- Content.ToObject<JsonElement>().TryGetProperty("data", out JsonElement content);
- return (200, $"{content}");
- }
- }
- else
- {
- string Content = await responseMessage.Content?.ReadAsStringAsync();
- return (500, Content);
- }
- }
- }
- public enum HttpTriggerUrl
- {
- [Description("localhost:7071,teammodelosfunction-test.chinacloudsites.cn,teammodelosfunction.chinacloudsites.cn,localhost:7071,teammodelfunction.azurewebsites.net")]
- HttpTrigger地址,
- }
- }
|