using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Net;
|
using System.Text;
|
using System.Web;
|
|
namespace IStation.Untity
|
{
|
/// <summary>
|
/// HTTP请求辅助类
|
/// </summary>
|
public class HttpRequestHelper
|
{
|
/// <summary>
|
/// 获取
|
/// </summary>
|
public static string Get(string url, Version version = null)
|
{
|
return Request(url, HttpMethod.Get, null, version);
|
}
|
|
/// <summary>
|
/// 提交
|
/// </summary>
|
public static string Post(string url, string data = null, Version version = null)
|
{
|
return Request(url, HttpMethod.Post, data, version);
|
}
|
|
/// <summary>
|
/// 更新
|
/// </summary>
|
public static string Put(string url, string data = null, Version version = null)
|
{
|
return Request(url, HttpMethod.Put, data, version);
|
}
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
public static string Delete(string url, string data = null, Version version = null)
|
{
|
return Request(url, HttpMethod.Delete, data, version);
|
}
|
|
//请求
|
private static string Request(string url, HttpMethod method, string data = null, Version version= null)
|
{
|
using (var httpClient = new HttpClient())
|
using (var request=new HttpRequestMessage(method,url))
|
{
|
if (version != null)
|
{
|
request.Version = version;
|
}
|
request.Headers.Add("KeepAlive","false");
|
if(!string.IsNullOrEmpty(data))
|
{
|
request.Content = new StringContent(data,Encoding.UTF8,"application/json");
|
}
|
var response=httpClient.SendAsync(request).Result;
|
response.EnsureSuccessStatusCode();
|
var responsetext=response.Content.ReadAsStringAsync().Result;
|
return responsetext;
|
}
|
}
|
|
|
}
|
}
|