Files
lightsetup/LightSetupMd5/Md5HttpRequest.cs
2023-05-22 09:29:58 +08:00

61 lines
2.1 KiB
C#

using LightSetupBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
namespace LightSetupMd5
{
internal class Md5HttpRequest
{
public static string Request(string url, string format, Dictionary<string, string> para)
{
if (String.IsNullOrEmpty(url))
{
return String.Empty;
}
string contentFrom = Md5Format.GetFormat(format, para);
using (var client = new HttpClient())
{
Task<HttpResponseMessage> task;
if (!String.IsNullOrEmpty(contentFrom))
{
Console.WriteLine("POST:" + url + ":" + contentFrom);
StringContent content;
if (contentFrom.StartsWith("{"))
{
content = new StringContent(contentFrom, Encoding.UTF8, "application/json");
}
else if (contentFrom.StartsWith("<"))
{
content = new StringContent(contentFrom, Encoding.UTF8, "application/xml");
}
else
{
content = new StringContent(contentFrom, Encoding.UTF8);
}
task = client.PostAsync(url, content);
}
else
{
Console.WriteLine("GET:" + url);
task = client.GetAsync(url);
}
task.Wait();
HttpResponseMessage response = task.GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();//用来抛异常的
Task<string> taskResult = response.Content.ReadAsStringAsync();
taskResult.Wait();
string responseBody = taskResult.GetAwaiter().GetResult();
Console.WriteLine("Response:" + url + ":" + responseBody);
return responseBody;
}
}
}
}