/// <summary> /// 带证书的post /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="obj"></param> /// <param name="cer"></param> /// <param name="result"></param> /// <param name="serializeStrFunc"></param> /// <param name="inputDataType"></param> /// <param name="outDataType"></param> /// <returns></returns> public T HttpPost <T>(string url, object obj, X509Certificate2 cer, out string result, Func <string, string> serializeStrFunc = null, WebRequestDataTypeEnums inputDataType = WebRequestDataTypeEnums.JSON, WebRequestDataTypeEnums outDataType = WebRequestDataTypeEnums.JSON, int timeout = 30000) where T : class { string postStr = null; this.TimeOut = timeout; switch (inputDataType) { case WebRequestDataTypeEnums.XML: postStr = XmlHelper.SerializeObject(obj); break; default: this.ContentType = "application/json; charset=UTF-8 "; postStr = JsonConvert.SerializeObject(obj); break; } if (serializeStrFunc != null) { postStr = serializeStrFunc(postStr); } result = HttpPost(url, postStr, cer); switch (outDataType) { case WebRequestDataTypeEnums.XML: return(XmlHelper.XmlDeserialize <T>(result)); default: return(JsonConvert.DeserializeObject <T>(result)); } }
///// <summary> ///// ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="url"></param> ///// <param name="obj"></param> ///// <returns></returns> //public T HttpPost<T>(string url, object obj, WebRequestDataTypes inputDataType = WebRequestDataTypes.JSON, WebRequestDataTypes outDataType = WebRequestDataTypes.JSON) //{ // var resultStr = HttpPost(url, JsonConvert.SerializeObject(obj)); // return JsonConvert.DeserializeObject<T>(resultStr); //} public T HttpPost <T>(string url, object obj, out string result, Encoding sendEncode = null, JsonSerializerSettings inputJsonSettings = null, Encoding receiveEncode = null, Func <string, string> serializeStrFunc = null, WebRequestDataTypeEnums inputDataType = WebRequestDataTypeEnums.JSON, WebRequestDataTypeEnums outDataType = WebRequestDataTypeEnums.JSON, int timeout = 30000) where T : class { try { string postStr = null; this.TimeOut = timeout; switch (inputDataType) { case WebRequestDataTypeEnums.JSON: postStr = XmlHelper.SerializeObject(obj); break; default: //request.ContentType = "text/html;charset=UTF-8"; this.ContentType = "application/json;charset=UTF-8 "; // postStr = JsonConvert.SerializeObject(obj, inputJsonSettings); break; } if (serializeStrFunc != null) { postStr = serializeStrFunc(postStr); } if (sendEncode == null) { sendEncode = Encoding.Default; } if (receiveEncode == null) { receiveEncode = Encoding.GetEncoding("gbk"); } result = HttpPost(url, postStr, sendEncode, receiveEncode); try { switch (outDataType) { case WebRequestDataTypeEnums.XML: return(XmlHelper.XmlDeserialize <T>(result)); default: return(JsonConvert.DeserializeObject <T>(result)); } } catch (Exception ea) { throw new Exception("解析出参[" + result + "]异常!" + ea.Message); } } catch (Exception ea) { throw ea; } }