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 { /// /// 请求类型 /// public enum EnumHttpVerb { GET, POST, PUT, DELETE } /// /// REST FUL接口类 /// public class RestClient { #region 属性 /// /// 端点路径 /// public string EndPoint { get; set; } /// /// 请求方式 /// public EnumHttpVerb Method { get; set; } /// /// 文本类型(1、application/json 2、txt/html) /// public string ContentType { get; set; } /// /// 请求的数据(一般为JSon格式) /// 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 方法 /// /// http请求(不带参数请求) /// /// public string HttpRequest() { return HttpRequest(""); } /// /// http请求(带参数) /// /// parameters例如:?name=LiLei /// 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 } }