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.

148 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace RS.Common
{
/// <summary>
/// 请求类型
/// </summary>
public enum EnumHttpVerb
{
GET,
POST,
PUT,
DELETE
}
/// <summary>
/// REST FUL接口类
/// </summary>
public class RestClient
{
#region 属性
/// <summary>
/// 端点路径
/// </summary>
public string EndPoint { get; set; }
/// <summary>
/// 请求方式
/// </summary>
public EnumHttpVerb Method { get; set; }
/// <summary>
/// 文本类型1、application/json 2、txt/html
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// 请求的数据(一般为JSon格式)
/// </summary>
public string PostData { get; set; }
#endregion
#region 初始化
public RestClient()
{
EndPoint = "";
Method = EnumHttpVerb.GET;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint)
{
EndPoint = endpoint;
Method = EnumHttpVerb.GET;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint, EnumHttpVerb method)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint, EnumHttpVerb method, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = postData;
}
#endregion
#region 方法
/// <summary>
/// http请求(不带参数请求)
/// </summary>
/// <returns></returns>
public string HttpRequest()
{
return HttpRequest("");
}
/// <summary>
/// http请求(带参数)
/// </summary>
/// <param name="parameters">parameters例如?name=LiLei</param>
/// <returns></returns>
public string HttpRequest(string parameters)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
if (!string.IsNullOrEmpty(PostData) && Method == EnumHttpVerb.POST)
{
var bytes = Encoding.UTF8.GetBytes(PostData);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = string.Format("请求数据失败. 返回的 HTTP 状态码:{0}", response.StatusCode);
throw new ApplicationException(message);
}
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
#endregion
}
}