RestClient
1 using System; 2 using System.Globalization; 3 using System.IO; 4 using System.Net; 5 using System.Text; 6 7 [assembly: CLSCompliant(true)] 8 namespace Downloader 9 { 10 public enum HttpVerb 11 { 12 Get, 13 Post, 14 Put, 15 Delete 16 } 17 18 /// <summary> 19 /// Restful Service Client 20 /// </summary> 21 public class RestClient 22 { 23 /// <summary> 24 /// application/json 25 /// </summary> 26 private string _contentType = "application/json"; 27 /// <summary> 28 /// 15000 29 /// </summary> 30 private int _timeout = 15000; 31 /// <summary> 32 /// Service Uri 33 /// </summary> 34 public string EndPoint { get; set; } 35 /// <summary> 36 /// Post || Get 37 /// </summary> 38 public HttpVerb Method { get; set; } 39 /// <summary> 40 /// Default "text/xml" 41 /// </summary> 42 public string ContentType { get { return _contentType; } set { _contentType = value; } } 43 /// <summary> 44 /// eg. "‘test‘:‘abcd‘" 45 /// </summary> 46 public string PostData { get; set; } 47 /// <summary> 48 /// TimeOut ms 49 /// </summary> 50 public int Timeout { get { return _timeout; } set { _timeout = value; } } 51 /// <summary> 52 /// Default Constructor 53 /// </summary> 54 public RestClient() 55 { 56 EndPoint = ""; 57 Method = HttpVerb.Get; 58 ContentType = _contentType; 59 PostData = "{}"; 60 Timeout = _timeout; 61 } 62 /// <summary> 63 /// Constructor 64 /// </summary> 65 /// <param name="endpoint">Service Uri</param> 66 public RestClient(string endpoint) 67 { 68 EndPoint = endpoint; 69 Method = HttpVerb.Get; 70 ContentType = _contentType; 71 PostData = "{}"; 72 Timeout = _timeout; 73 } 74 /// <summary> 75 /// Constructor 76 /// </summary> 77 /// <param name="endpoint">Service Uri</param> 78 /// <param name="method">Post || Get</param> 79 public RestClient(string endpoint, HttpVerb method) 80 { 81 EndPoint = endpoint; 82 Method = method; 83 ContentType = _contentType; 84 PostData = "{}"; 85 Timeout = _timeout; 86 } 87 /// <summary> 88 /// Constructor 89 /// </summary> 90 /// <param name="endpoint">Service Uri</param> 91 /// <param name="method">Post || Get</param> 92 /// <param name="postData">eg. "{‘test‘:‘abcd‘}"</param> 93 public RestClient(string endpoint, HttpVerb method, string postData) 94 { 95 EndPoint = endpoint; 96 Method = method; 97 ContentType = _contentType; 98 PostData = postData; 99 Timeout = _timeout; 100 } 101 /// <summary> 102 /// Request server 103 /// </summary> 104 /// <returns></returns> 105 public string MakeRequest() 106 { 107 return MakeRequest(""); 108 } 109 /// <summary> 110 /// Request server 111 /// </summary> 112 /// <param name="parameters">eg. "?q=1234"</param> 113 /// <returns></returns> 114 public string MakeRequest(string parameters) 115 { 116 var service = new Uri(EndPoint + parameters); 117 var request = WebRequest.Create(service); 118 request.Timeout = Timeout; 119 request.Method = Method.ToString().ToUpper(CultureInfo.CurrentCulture); 120 request.ContentLength = 0; 121 request.ContentType = ContentType; 122 123 if (Method == HttpVerb.Post) 124 { 125 PostData = "{" + PostData + "}"; 126 var bytes =Encoding.UTF8.GetBytes(PostData); 127 request.ContentLength = bytes.Length; 128 129 using (var writeStream = request.GetRequestStream()) 130 { 131 writeStream.Write(bytes, 0, bytes.Length); 132 } 133 } 134 135 using (var response = (HttpWebResponse)request.GetResponse()) 136 { 137 var responseValue = string.Empty; 138 if (response.StatusCode != HttpStatusCode.OK) 139 { 140 var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode); 141 throw new WebException(message); 142 } 143 using (var responseStream = response.GetResponseStream()) 144 { 145 if (responseStream != null) 146 using (var reader = new StreamReader(responseStream)) 147 { 148 responseValue = reader.ReadToEnd(); 149 } 150 } 151 return responseValue; 152 } 153 } 154 /// <summary> 155 /// 同步更新二进制文件 156 /// </summary> 157 /// <param name="parameters">URL参数</param> 158 /// <param name="postData">表单提交参数</param> 159 /// <returns></returns> 160 public byte[] MakeRequest(string parameters, string postData) 161 { 162 var service = new Uri(EndPoint + parameters); 163 var request = WebRequest.Create(service); 164 request.Timeout = Timeout; 165 request.Method = Method.ToString().ToUpper(CultureInfo.CurrentCulture); 166 request.ContentLength = 0; 167 request.ContentType = ContentType; 168 169 if (Method == HttpVerb.Post) 170 { 171 PostData = "{" + postData + "}"; 172 var bytes = Encoding.UTF8.GetBytes(PostData); 173 request.ContentLength = bytes.Length; 174 175 using (var writeStream = request.GetRequestStream()) 176 { 177 writeStream.Write(bytes, 0, bytes.Length); 178 } 179 } 180 181 using (var response = (HttpWebResponse)request.GetResponse()) 182 { 183 if (response.StatusCode != HttpStatusCode.OK) 184 { 185 var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode); 186 throw new WebException(message); 187 } 188 using (var result = new MemoryStream()) 189 { 190 const int bufferLen = 2048; 191 var buffer = new byte[bufferLen]; 192 using (var responseStream = response.GetResponseStream()) 193 { 194 var len = responseStream.Read(buffer, 0, bufferLen); 195 while (len > 0) 196 { 197 result.Write(buffer, 0, len); 198 len = responseStream.Read(buffer, 0, bufferLen); 199 } 200 } 201 return result.ToArray(); 202 } 203 } 204 } 205 206 } 207 208 }
时间: 2024-10-09 08:00:52