public static string DoJsonRequest <T>(string strUri, EnuHttpMethod method, T objToSend, IUriConvertable queryCondition = null, long tick = 0) where T : class { string strToRequest = strUri; if (tick != 0 && (method == EnuHttpMethod.Put || method == EnuHttpMethod.Delete)) { strToRequest += "?UpdateTicks=" + tick.ToString(CultureInfo.InvariantCulture); } if (queryCondition != null && method == EnuHttpMethod.Get) { strToRequest += queryCondition.QueryString; } WebRequest requestMsg = WebRequest.Create(strToRequest); MakePrincipleHeader(requestMsg, strToRequest); requestMsg.ContentType = "application/json"; requestMsg.Proxy = null; switch (method) { case EnuHttpMethod.Delete: requestMsg.Method = "DELETE"; break; case EnuHttpMethod.Put: requestMsg.Method = "PUT"; break; case EnuHttpMethod.Post: requestMsg.Method = "POST"; break; default: //EnuHttpMethod.Get: requestMsg.Method = "GET"; break; } if (objToSend != null) { //IsoDateTimeConverter dt = new IsoDateTimeConverter(); //dt.DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss"; //string postData = JsonConvert.SerializeObject(objToSend, dt); byte[] byteArray; if (typeof(T).Name == "List`1") { IsoDateTimeConverter dt = new IsoDateTimeConverter(); dt.DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss"; StringBuilder sb = new StringBuilder(); var listObj = (IEnumerable)objToSend; sb.Append("DataMetaType=List&"); int i = 0; foreach (var item in listObj) { sb.Append("List" + (i++).ToString()); sb.Append("="); sb.Append(JsonConvert.SerializeObject(item, dt)); sb.Append("&"); } sb.Append("DataMetaCount=" + i.ToString()); byteArray = Encoding.UTF8.GetBytes(sb.ToString()); } else { byteArray = Encoding.UTF8.GetBytes(ParseToString <T>(objToSend)); } requestMsg.ContentType = "application/x-www-form-urlencoded"; requestMsg.ContentLength = byteArray.Length; Stream dataStream = requestMsg.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); } HttpWebResponse response = null; string responseFromServer = ""; try { using (response = (HttpWebResponse)requestMsg.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { responseFromServer = reader.ReadToEnd(); } } } catch (WebException ex) { response = (HttpWebResponse)ex.Response; responseFromServer = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd(); throw new ClientException(responseFromServer, ex, response.StatusCode); } return(responseFromServer); }
public static string DoFileRequest(string strUri, EnuHttpMethod method, string filePath, long tick = 0) { string strToRequest = strUri; if (tick != 0) { strToRequest += "?UpdateTicks=" + tick.ToString(CultureInfo.InvariantCulture); } WebRequest requestMsg = WebRequest.Create(strToRequest); MakePrincipleHeader(requestMsg, strToRequest); switch (method) { case EnuHttpMethod.Delete: requestMsg.Method = "DELETE"; break; case EnuHttpMethod.Put: case EnuHttpMethod.Post: requestMsg.Method = "POST"; break; default: //EnuHttpMethod.Get: requestMsg.Method = "GET"; break; } if (method == EnuHttpMethod.Post || method == EnuHttpMethod.Put) { if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) { throw new ClientException("本地文件不存在,不能读取!"); } FileStream fileStream = null; Stream requestStream = null; try { fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); var fileName = GetFileName(filePath); string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); requestMsg.ContentType = "multipart/form-data;boundary=" + boundary; requestMsg.Headers.Add("FileName", HttpUtility.UrlEncode(fileName));//额外自定义文件名放在头中 requestMsg.ContentLength = fileStream.Length; requestStream = requestMsg.GetRequestStream(); byte[] buffer = new Byte[checked ((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } fileStream.Close(); } catch (Exception e) { if (requestStream != null) { requestStream.Close(); } if (fileStream != null) { fileStream.Close(); } throw new ClientException("本地文件异常,不能读取!"); } } else { requestMsg.ContentType = "application/json"; } try { using (HttpWebResponse response = (HttpWebResponse)requestMsg.GetResponse()) { if (method == EnuHttpMethod.Post || method == EnuHttpMethod.Put) { //返回上传之后的文件名 using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string responseFromServer = reader.ReadToEnd(); if (requestMsg != null) { requestMsg.Abort(); } return(responseFromServer); } } else { //返回文件流 using (Stream stream = response.GetResponseStream()) { if (File.Exists(filePath)) { throw new ClientException("本地文件已经存在,不能覆盖!"); } using (FileStream fs = File.Create(filePath)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) { fs.Write(buffer, 0, bytesRead); } fs.Close(); } } return(""); } } } catch (IOException e) { throw new ClientException("本地文件写失败!"); } catch (WebException ex) { if (requestMsg != null) { requestMsg.Abort(); } var response = (HttpWebResponse)ex.Response; var msg = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd(); throw new ClientException(msg, ex, response.StatusCode); } }
public static string DoJsonRequest(string strUri, EnuHttpMethod method, IUriConvertable queryCondition = null, long tick = 0) { return(DoJsonRequest <object>(strUri, method, null, queryCondition: queryCondition, tick: tick)); }
public static T DoJsonRequest <T>(string strUri, EnuHttpMethod method, IUriConvertable queryCondition = null, Object objToSend = null, long tick = 0) { string strToRequest = strUri; if (tick != 0 && (method == EnuHttpMethod.Put || method == EnuHttpMethod.Delete)) { strToRequest += "?UpdateTicks=" + tick.ToString(CultureInfo.InvariantCulture); } if (queryCondition != null && method == EnuHttpMethod.Get) { strToRequest += queryCondition.QueryString; } HttpRequestMessage requestMsg = new HttpRequestMessage(); MakePrincipleHeader(requestMsg, strToRequest); requestMsg.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); requestMsg.RequestUri = new Uri(strToRequest); HttpContent content = null; if (objToSend != null) { content = new StringContent(JsonConvert.SerializeObject(objToSend), Encoding.UTF8, "application/json"); } switch (method) { case EnuHttpMethod.Post: requestMsg.Method = HttpMethod.Post; requestMsg.Content = content; break; case EnuHttpMethod.Put: requestMsg.Method = HttpMethod.Put; requestMsg.Content = content; break; case EnuHttpMethod.Delete: requestMsg.Method = HttpMethod.Delete; break; default: //EnuHttpMethod.Get: requestMsg.Method = HttpMethod.Get; break; } Task <HttpResponseMessage> rtnAll = s_httpClient.SendAsync(requestMsg); #region 执行 HttpResponseMessage resultMessage = null; Task <T> rtnFinal; try { try { resultMessage = rtnAll.Result; } catch (AggregateException ae) { foreach (var ex in ae.InnerExceptions) { if (ex is HttpRequestException) { throw new ClientException(ERRMSG_FAILED_TO_SEND_MESSAGE, ex); } } } if (!resultMessage.IsSuccessStatusCode) { Task <WebApiHttpInfo> error; try { error = resultMessage.Content.ReadAsAsync <WebApiHttpInfo>(s_formatters); } catch (Exception ex) { throw new ClientException(ERRMSG_SERVER_EXCEPTIONAL_FAILURE, ex); } throw new ClientException(error.Result.Message); } if (method != EnuHttpMethod.Get) { return(default(T)); } try { rtnFinal = resultMessage.Content.ReadAsAsync <T>(s_formatters); } catch (Exception ex) { throw new ClientException(ERRMSG_RESULT_CONTENT_DOESNOT_MATCH, ex); } } catch (ClientException ex) { ex.ParameterObject = new { Uri = strUri, Method = method, Data = objToSend, Tick = tick }; throw; } #endregion return(rtnFinal.Result); }