2025-04-01 13:41:50 +08:00

53 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace RIZO.Client.DAL
{
public class WebDataAccess
{
protected string domain = "http://localhost:5000/api/";
public Task<string> GetDatas(string url)
{
using (HttpClient client = new HttpClient())
{
var resp = client.GetAsync(url).GetAwaiter().GetResult();
return resp.Content.ReadAsStringAsync();
}
}
private MultipartFormDataContent GetFormData(Dictionary<string, HttpContent> contents)
{
var postContent = new MultipartFormDataContent();
string boundary = $"-------{DateTime.Now.Ticks.ToString("x")}-----------";
postContent.Headers.Add("ContentType", $"muiltipart/form-data,boundary={boundary}");
foreach (var item in contents)
{
postContent.Add(item.Value, item.Key);
}
return postContent;
}
public Task<string> PostDatas(string url, Dictionary<string, HttpContent> contents)
{
using (HttpClient client = new HttpClient())
{
var resp = client.PostAsync(url, this.GetFormData(contents)).GetAwaiter().GetResult();
return resp.Content.ReadAsStringAsync();
}
}
public Task<string> PostDatas(string url, HttpContent contents)
{
using (HttpClient client = new HttpClient())
{
var resp = client.PostAsync(url, contents).GetAwaiter().GetResult();
return resp.Content.ReadAsStringAsync();
}
}
}
}