步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.3 KiB

2 years ago
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
2 years ago
using System.Windows;
using System.Linq;
2 years ago
namespace BBWY.Client.Helpers
{
public class HttpClientHelper
{
public HttpClientHelper(string baseAddr)
{
this.BaseAddr = baseAddr;
}
public string BaseAddr { get; set; }
public string Get(string Url)
{
2 years ago
try
2 years ago
{
2 years ago
HttpClient client = new HttpClient();
//设置 API的 基地址
client.BaseAddress = new Uri(BaseAddr);
//设置 默认请求头ACCEPT
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//发送GET请求
HttpResponseMessage msg = client.GetAsync(Url).Result;
//判断结果是否成功
if (msg.IsSuccessStatusCode)
{
//返回响应结果
return msg.Content.ReadAsStringAsync().Result;
}
2 years ago
}
2 years ago
catch (Exception)
{
MessageBox.Show("网络异常,无法连接到服务器");
}
2 years ago
//返回空字符串,表示响应错误
return "";
}
public string Delete(string Url)
{
HttpClient client = new HttpClient();
//设置 API的 基地址
client.BaseAddress = new Uri(BaseAddr);
//设置 默认请求头ACCEPT
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//发送GET请求
HttpResponseMessage msg = client.DeleteAsync(Url).Result;
//判断结果是否成功
if (msg.IsSuccessStatusCode)
{
//返回响应结果
return msg.Content.ReadAsStringAsync().Result;
}
//返回空字符串,表示响应错误
return "";
}
public string Post(string Url, string JsonData)
{
2 years ago
try
2 years ago
{
2 years ago
HttpClient client = new HttpClient();
//设置 API的 基地址
client.BaseAddress = new Uri(BaseAddr);
//设置 默认请求头ACCEPT
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//设置消息体
HttpContent content = new StringContent(JsonData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//发送Post请求
HttpResponseMessage msg = client.PostAsync(Url, content).Result;
//判断结果是否成功
if (msg.IsSuccessStatusCode)
{
//返回响应结果
return msg.Content.ReadAsStringAsync().Result;
}
}
catch (Exception)
{
MessageBox.Show("网络异常,无法连接到服务器");
2 years ago
}
2 years ago
2 years ago
//返回空字符串,表示响应错误
return "";
}
public string Put(string Url, string JsonData)
{
HttpClient client = new HttpClient();
//设置 API的 基地址
client.BaseAddress = new Uri(BaseAddr);
//设置 默认请求头ACCEPT
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//设置消息体
HttpContent content = new StringContent(JsonData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//发送Post请求
HttpResponseMessage msg = client.PutAsync(Url, content).Result;
//判断结果是否成功
if (msg.IsSuccessStatusCode)
{
//返回响应结果
return msg.Content.ReadAsStringAsync().Result;
}
//返回空字符串,表示响应错误
return "";
}
public T Request<T>(string type, string url, object data = null)
{
string json = JsonConvert.SerializeObject(data);
string result = "";
switch (type)
{
case "Get":
result = Get(url);
break;
case "Post":
result = Post(url, json);
break;
case "Put":
result = Put(url, json);
break;
case "Delete":
result = Delete(url);
break;
}
return JsonConvert.DeserializeObject<T>(result);
}
public string Post(string Url, string JsonData, string headers = null)
{
HttpClient client = new HttpClient();
//设置 API的 基地址
client.BaseAddress = new Uri(BaseAddr);
if (!string.IsNullOrEmpty(headers))
{
var headdics = JsonConvert.DeserializeObject<Dictionary<string, string>>(headers);
foreach (var item in headdics.Keys)
{
if (item.ToLower() == "host" || item.ToLower() == "Content-Type".ToLower() || item.ToLower() == "Content-Length".ToLower()) continue;
client.DefaultRequestHeaders.Add(item, headdics[item]);
}
}
//设置 默认请求头ACCEPT
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//设置消息体
HttpContent content = new StringContent(JsonData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//发送Post请求
HttpResponseMessage msg = client.PostAsync(Url, content).Result;
//判断结果是否成功
if (msg.IsSuccessStatusCode)
{
//返回响应结果
return msg.Content.ReadAsStringAsync().Result;
}
//返回空字符串,表示响应错误
return "";
}
2 years ago
}
}