/// <summary> /// POST请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public static string SendRequestData(string url, string data, ref HttpWebResponse response, CertificateEntity Certificate = null) { byte[] byteArray = Encoding.UTF8.GetBytes(data); byte[] resultdata = SendRequestData(url, byteArray, ref response, Certificate); string result = Encoding.UTF8.GetString(resultdata); return(result); }
public static string SendRequestData <T>(string url, T data, ref HttpWebResponse response, CertificateEntity Certificate = null) where T : class { return(SendRequestData(url, SerializerJson.SerializeObject(data), ref response, Certificate)); }
public static byte[] SendRequestData(string url, byte[] byteArray, ref HttpWebResponse response, CertificateEntity Certificate = null) { byte[] result = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url)); if (Certificate != null) { X509Certificate2 cer = new X509Certificate2(Certificate.CFileName, Certificate.PassWord, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); request.ClientCertificates.Add(cer); } if (Header != null && Header.Count > 0) { foreach (DictionaryEntry header in Header) { request.Headers.Add(header.Key.ToString(), header.Value.ToString()); } } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream stream = request.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); //写入参数 stream.Close(); response = (HttpWebResponse)request.GetResponse(); Stream responsestream = response.GetResponseStream(); if (responsestream.CanRead) { //将基础流写入内存流 MemoryStream memoryStream = new MemoryStream(); const int bufferLength = 1024; int actual; byte[] buffer = new byte[bufferLength]; while ((actual = responsestream.Read(buffer, 0, bufferLength)) > 0) { memoryStream.Write(buffer, 0, actual); } memoryStream.Position = 0; result = StreamToBytes(memoryStream); memoryStream.Close(); } responsestream.Close(); } catch (Exception ex) { throw ex; } return(result); }