public static ClientResult InvokeApi(string url, List<ParmField> aparmList) { // File.AppendAllText("D:\\crm.invoke.api.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + url +" ==>start \r\n"); if (!url.StartsWith("http")) { string tempt = System.Configuration.ConfigurationManager.AppSettings["appurl"]; if (tempt.EndsWith("/")) { tempt = tempt.Remove(tempt.Length - 1, 1); } if (!url.StartsWith("/")) { url = "/" + url; } url = tempt + url; } ClientResult result = new ClientResult(); HttpClient client = new HttpClient(); Task<HttpResponseMessage> resultTask; try { List<ParmField> curParmList = new List<ParmField>(); curParmList = aparmList; //curParmList.Add(new StringField("sign", sign)); string boundary = "------------------------------7dXiaoXiao"; byte[] bodybyte = getBody(curParmList, boundary); HttpContent content = new ByteArrayContent(bodybyte, 0, bodybyte.Length); content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data;boundary=" + boundary); //string httpcode = WebLib.Sys.BytesToStr(bodybyte); resultTask = client.PostAsync(url, content); resultTask.Wait(); result.statuscode = (int)resultTask.Result.StatusCode; if (result.statuscode == 404) { result.code = -100; result.msg = "您正在查找的资源不可用!"; result.resString = "您正在查找的资源不可用!"; return result; } try { result.responseContentType = resultTask.Result.Content.Headers.ContentType.MediaType.ToLower(); } catch { result.responseContentType = "applicaiton/octet-stream"; } if (result.responseContentType == "application/json" || result.responseContentType == "text/html") { Task<string> strTask = resultTask.Result.Content.ReadAsStringAsync(); strTask.Wait(); result.resString = strTask.Result; result.total = 0; if (result.statuscode == 200) { try { result.repObject = JObject.Parse(result.resString); result.code = result.repObject["code"].Value<int>(); result.msg = result.repObject["msg"] != null ? result.repObject["msg"].Value<string>() : ""; result.total = result.repObject["total"] != null ? result.repObject["total"].Value<long>() : 0L; } catch// (Exception ex) { result.code = -100; result.msg = "返回json解析出错,源json请查看result.resString."; } } else { result.code = -100; result.msg = "请求出错,请查看result.statuscode"; } return result; } else { //文件流返回, //result.code = -100; //result.msg = "此外为文件下载,如需要此功能,请编写。如非文件下载,请检查 result.responseContentType "; // return result; #region result.code = 1; Task<Stream> streamTask = resultTask.Result.Content.ReadAsStreamAsync(); streamTask.Wait(); result.responseStream = streamTask.Result; result.responseContentType = resultTask.Result.Content.Headers.ContentType.MediaType; if (result.responseStream == null) { result.code = -100; result.statuscode = -1; result.msg = "下载文件时发生错误!"; return result; } else { if (resultTask.Result.Content.Headers.ContentDisposition.FileName == null) result.resString = resultTask.Result.Content.Headers.ContentDisposition.FileNameStar; else result.resString = resultTask.Result.Content.Headers.ContentDisposition.FileName; return result; } #endregion } } catch (Exception ex) { result.code = -100; if (ex.InnerException != null) { if (ex.InnerException is System.Net.Http.HttpRequestException) { result.msg = "连接服务器[" + url + "]时发生错误!"; } else { result.msg = ex.InnerException.Message; } } else { result.msg = ex.Message; } return result; } finally { // File.AppendAllText("D:\\crm.invoke.api.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + url + " ==>end\r\n"); client.Dispose(); } }
public static ClientResult InvokeApi(string url, List <ParmField> aparmList) { // File.AppendAllText("D:\\crm.invoke.api.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + url +" ==>start \r\n"); if (!url.StartsWith("http")) { string tempt = System.Configuration.ConfigurationManager.AppSettings["appurl"]; if (tempt.EndsWith("/")) { tempt = tempt.Remove(tempt.Length - 1, 1); } if (!url.StartsWith("/")) { url = "/" + url; } url = tempt + url; } ClientResult result = new ClientResult(); HttpClient client = new HttpClient(); Task <HttpResponseMessage> resultTask; try { List <ParmField> curParmList = new List <ParmField>(); curParmList = aparmList; //curParmList.Add(new StringField("sign", sign)); string boundary = "------------------------------7dXiaoXiao"; byte[] bodybyte = getBody(curParmList, boundary); HttpContent content = new ByteArrayContent(bodybyte, 0, bodybyte.Length); content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data;boundary=" + boundary); //string httpcode = WebLib.Sys.BytesToStr(bodybyte); resultTask = client.PostAsync(url, content); resultTask.Wait(); result.statuscode = (int)resultTask.Result.StatusCode; if (result.statuscode == 404) { result.code = -100; result.msg = "您正在查找的资源不可用!"; result.resString = "您正在查找的资源不可用!"; return(result); } try { result.responseContentType = resultTask.Result.Content.Headers.ContentType.MediaType.ToLower(); } catch { result.responseContentType = "applicaiton/octet-stream"; } if (result.responseContentType == "application/json" || result.responseContentType == "text/html") { Task <string> strTask = resultTask.Result.Content.ReadAsStringAsync(); strTask.Wait(); result.resString = strTask.Result; result.total = 0; if (result.statuscode == 200) { try { result.repObject = JObject.Parse(result.resString); result.code = result.repObject["code"].Value <int>(); result.msg = result.repObject["msg"] != null ? result.repObject["msg"].Value <string>() : ""; result.total = result.repObject["total"] != null ? result.repObject["total"].Value <long>() : 0L; } catch// (Exception ex) { result.code = -100; result.msg = "返回json解析出错,源json请查看result.resString."; } } else { result.code = -100; result.msg = "请求出错,请查看result.statuscode"; } return(result); } else { //文件流返回, //result.code = -100; //result.msg = "此外为文件下载,如需要此功能,请编写。如非文件下载,请检查 result.responseContentType "; // return result; #region result.code = 1; Task <Stream> streamTask = resultTask.Result.Content.ReadAsStreamAsync(); streamTask.Wait(); result.responseStream = streamTask.Result; result.responseContentType = resultTask.Result.Content.Headers.ContentType.MediaType; if (result.responseStream == null) { result.code = -100; result.statuscode = -1; result.msg = "下载文件时发生错误!"; return(result); } else { if (resultTask.Result.Content.Headers.ContentDisposition.FileName == null) { result.resString = resultTask.Result.Content.Headers.ContentDisposition.FileNameStar; } else { result.resString = resultTask.Result.Content.Headers.ContentDisposition.FileName; } return(result); } #endregion } } catch (Exception ex) { result.code = -100; if (ex.InnerException != null) { if (ex.InnerException is System.Net.Http.HttpRequestException) { result.msg = "连接服务器[" + url + "]时发生错误!"; } else { result.msg = ex.InnerException.Message; } } else { result.msg = ex.Message; } return(result); } finally { // File.AppendAllText("D:\\crm.invoke.api.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + url + " ==>end\r\n"); client.Dispose(); } }