/// <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, WebRequestDataTypes inputDataType = WebRequestDataTypes.JSON, WebRequestDataTypes outDataType = WebRequestDataTypes.JSON) where T : class { string postStr = null; switch (inputDataType) { case WebRequestDataTypes.XML: postStr = XmlHelper.SerializeObject(obj); break; default: postStr = JsonConvert.SerializeObject(obj); break; } if (serializeStrFunc != null) { postStr = serializeStrFunc(postStr); } result = HttpPost(url, postStr, cer); switch (outDataType) { case WebRequestDataTypes.XML: return(XmlHelper.DeserializeObject <T>(result)); default: return(JsonConvert.DeserializeObject <T>(result)); } }
public T HttpPost <T>(string url, object obj, out string result, Func <string, string> serializeStrFunc = null, WebRequestDataTypes inputDataType = WebRequestDataTypes.JSON, WebRequestDataTypes outDataType = WebRequestDataTypes.JSON) where T : class { string postStr = null; switch (inputDataType) { case WebRequestDataTypes.XML: postStr = XmlHelper.SerializeObject(obj); break; default: postStr = JsonConvert.SerializeObject(obj); break; } if (serializeStrFunc != null) { postStr = serializeStrFunc(postStr); } WeChatHelper.ApiLogger.LogFormat(Logger.LoggerLevels.Trace, "Pre POST Url:{0},Data:{1}", url, postStr); result = HttpPost(url, postStr); WeChatHelper.ApiLogger.LogFormat(Logger.LoggerLevels.Trace, "POST Url:{0},result:{1}", url, result); switch (outDataType) { case WebRequestDataTypes.XML: return(XmlHelper.DeserializeObject <T>(result)); default: return(JsonConvert.DeserializeObject <T>(result)); } }
/// <summary> /// POST请求 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="obj"></param> /// <param name="serializeStrFunc"></param> /// <param name="inputDataType"></param> /// <param name="outDataType"></param> /// <param name="cer"></param> /// <returns></returns> public static async Task <T> PostAsync <T>(string url, object obj, Func <string, string> serializeStrFunc = null, WebRequestDataTypes inputDataType = WebRequestDataTypes.JSON, WebRequestDataTypes outDataType = WebRequestDataTypes.JSON, X509Certificate2 cer = null) where T : class { string postStr; switch (inputDataType) { case WebRequestDataTypes.XML: postStr = Xml.SerializeObjectWithoutNamespace(obj); break; default: postStr = JsonConvert.SerializeObject(obj); break; } if (serializeStrFunc != null) { postStr = serializeStrFunc(postStr); } string result; if (cer.IsNullOrEmpty()) { result = await PostAsync(url, postStr); } else { result = await PostAsync(url, postStr, cer); } switch (outDataType) { case WebRequestDataTypes.XML: return(Xml.DeserializeObject <T>(result)); default: return(JsonConvert.DeserializeObject <T>(result)); } }
public T HttpPost <T>(string url, string fileName, Stream fileStream, out string result, WebRequestDataTypes outDataType = WebRequestDataTypes.JSON) where T : class { var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); var boundarybytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); var endbytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); var request = CreateWebRequest(url); request.ContentType = "multipart/form-data; boundary=" + boundary; request.KeepAlive = true; request.AllowAutoRedirect = AllowAutoRedirect; request.Method = "POST"; if (Cookie != null) { request.CookieContainer = Cookie; } request.Credentials = CredentialCache.DefaultCredentials; using (var requestStream = request.GetRequestStream()) { var headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; var buffer = new byte[4096]; var bytesRead = 0; requestStream.Write(boundarybytes, 0, boundarybytes.Length); var header = string.Format(headerTemplate, fileName, fileName); var headerbytes = Encoding.UTF8.GetBytes(header); requestStream.Write(headerbytes, 0, headerbytes.Length); while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } requestStream.Write(endbytes, 0, endbytes.Length); } using (var response = (HttpWebResponse)request.GetResponse()) { using (var sr = new StreamReader(response.GetResponseStream())) { result = sr.ReadToEnd(); } } switch (outDataType) { case WebRequestDataTypes.XML: return(XmlHelper.DeserializeObject <T>(result)); default: return(JsonConvert.DeserializeObject <T>(result)); } }