|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace BBWY.Client.Helpers
|
|
|
|
{
|
|
|
|
public class HttpClientHelper
|
|
|
|
{
|
|
|
|
public HttpClientHelper(string baseAddr)
|
|
|
|
{
|
|
|
|
this.BaseAddr = baseAddr;
|
|
|
|
}
|
|
|
|
public string BaseAddr { get; set; }
|
|
|
|
|
|
|
|
public string Get(string Url)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
MessageBox.Show("网络异常,无法连接到服务器");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//返回空字符串,表示响应错误
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
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("网络异常,无法连接到服务器");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//返回空字符串,表示响应错误
|
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|