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.
122 lines
4.3 KiB
122 lines
4.3 KiB
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
|
|
namespace BBWY.Client.Helpers
|
|
{
|
|
public class HttpClientHelper
|
|
{
|
|
public HttpClientHelper(string baseAddr)
|
|
{
|
|
this.BaseAddr = baseAddr;
|
|
}
|
|
public string BaseAddr { get; set; }
|
|
|
|
public string Get(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.GetAsync(Url).Result;
|
|
//判断结果是否成功
|
|
if (msg.IsSuccessStatusCode)
|
|
{
|
|
//返回响应结果
|
|
return msg.Content.ReadAsStringAsync().Result;
|
|
}
|
|
//返回空字符串,表示响应错误
|
|
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)
|
|
{
|
|
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;
|
|
}
|
|
//返回空字符串,表示响应错误
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
|