/// <summary> /// Form表单Post数据,获取JSON /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="cookieContainer">CookieContainer,如果不需要则设为null</param> /// <param name="formData">表单数据,Key对应name,Value对应value</param> /// <param name="encoding"></param> /// <param name="certName">证书唯一名称,如果不需要则保留null</param> /// <param name="cer">证书,如果不需要则保留null</param> /// <param name="useAjax">是否使用Ajax请求</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <param name="afterReturnText">返回JSON本文,并在进行序列化之前触发,参数分别为:url、returnText</param> /// <returns></returns> public static T PostGetJson <T>(string url, CookieContainer cookieContainer = null, Dictionary <string, string> formData = null, Encoding encoding = null, string certName = null, bool useAjax = false, Action <string, string> afterReturnText = null, int timeOut = OFoodConfig.TIME_OUT) { string returnText = RequestUtility.HttpPost(url, cookieContainer, formData, encoding, certName, useAjax, null, timeOut); //SenparcTrace.SendApiLog(url, returnText); afterReturnText?.Invoke(url, returnText); var result = SerializerHelper.GetObject <T>(returnText); return(result); }
/// <summary> /// 发起Post请求,可上传文件 /// </summary> /// <typeparam name="T">返回数据类型(Json对应的实体)</typeparam> /// <param name="url">请求Url</param> /// <param name="cookieContainer">CookieContainer,如果不需要则设为null</param> /// <param name="encoding"></param> /// <param name="certName">证书唯一名称,如果不需要则保留null</param> /// <param name="cer">证书,如果不需要则保留null</param> /// <param name="useAjax"></param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <param name="fileDictionary">需要Post的文件(Dictionary 的 Key=name,Value=绝对路径)</param> /// <param name="postDataDictionary">需要Post的键值对(name,value)</param> /// <param name="afterReturnText">返回JSON本文,并在进行序列化之前触发,参数分别为:url、returnText</param> /// <returns></returns> public static T PostFileGetJson <T>(string url, CookieContainer cookieContainer = null, Dictionary <string, string> fileDictionary = null, Dictionary <string, string> postDataDictionary = null, Encoding encoding = null, string certName = null, bool useAjax = false, Action <string, string> afterReturnText = null, int timeOut = OFoodConfig.TIME_OUT) { using (MemoryStream ms = new MemoryStream()) { postDataDictionary.FillFormDataStream(ms); //填充formData string returnText = RequestUtility.HttpPost(url, cookieContainer, ms, fileDictionary, null, encoding, certName, useAjax, null, timeOut); afterReturnText?.Invoke(url, returnText); var result = SerializerHelper.GetObject <T>(returnText); return(result); } }