HttpMethods (C#.Net)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net; using log4net; namespace FaceppSDK { #region 提交方式 class HttpMethods : IHttpMethod { readonly ILog logger = log4net.LogManager.GetLogger(typeof(HttpMethods)); #region POST /// <summary> /// HTTP POST方式请求数据 /// </summary> /// <param name="url">URL.</param> /// <param name="param">POST的数据</param> /// <returns></returns> public virtual string HttpPost(string url, string param) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "*/*"; request.Timeout = 150000; request.AllowAutoRedirect = false; StreamWriter requestStream = null; WebResponse response = null; string responseStr = null; try { requestStream = new StreamWriter(request.GetRequestStream()); requestStream.Write(param); requestStream.Close(); response = request.GetResponse(); if (response != null) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); responseStr = reader.ReadToEnd(); reader.Close(); } } catch (Exception) { logger.Error("api 访问错误"); } finally { request = null; requestStream = null; response = null; } return responseStr; } #endregion #region Get /// <summary> /// HTTP GET方式请求数据. /// </summary> /// <param name="url">URL.</param> /// <returns></returns> public virtual string HttpGet(string url) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; //request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "*/*"; request.Timeout = 150000; request.AllowAutoRedirect = false; WebResponse response = null; string responseStr = null; try { response = request.GetResponse(); if (response != null) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); responseStr = reader.ReadToEnd(); reader.Close(); } } catch (Exception) { logger.Error("api 访问错误"); } finally { request = null; response = null; } return responseStr; } #endregion #region Post With Pic private string HttpPost(string url, IDictionary<object, object> param, string filePath) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.Method = "POST"; wr.KeepAlive = true; wr.Credentials = System.Net.CredentialCache.DefaultCredentials; Stream rs = wr.GetRequestStream(); string responseStr = null; string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string key in param.Keys) { rs.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, param[key]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length); } rs.Write(boundarybytes, 0, boundarybytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, "img", filePath, "text/plain"); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { rs.Write(buffer, 0, bytesRead); } fileStream.Close(); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); rs.Write(trailer, 0, trailer.Length); rs.Close(); WebResponse wresp = null; try { wresp = wr.GetResponse(); Stream stream2 = wresp.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); responseStr = reader2.ReadToEnd(); logger.Debug(string.Format("File uploaded, server response is: {0}", responseStr)); } catch (Exception ex) { logger.Error("Error uploading file", ex); if (wresp != null) { wresp.Close(); wresp = null; } } finally { wr = null; } return responseStr; } #endregion #region Post With Pic /// <summary> /// HTTP POST方式请求数据(带图片) /// </summary> /// <param name="url">URL</param> /// <param name="param">POST的数据</param> /// <param name="fileByte">图片</param> /// <returns></returns> public virtual string HttpPost(string url, IDictionary<object, object> param, byte[] fileByte) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.Method = "POST"; wr.KeepAlive = true; wr.Credentials = System.Net.CredentialCache.DefaultCredentials; Stream rs = wr.GetRequestStream(); string responseStr = null; string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string key in param.Keys) { rs.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, param[key]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length); } rs.Write(boundarybytes, 0, boundarybytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, "img", fileByte, "text/plain");//image/jpeg byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); rs.Write(fileByte, 0, fileByte.Length); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); rs.Write(trailer, 0, trailer.Length); rs.Close(); WebResponse wresp = null; try { wresp = wr.GetResponse(); Stream stream2 = wresp.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); responseStr = reader2.ReadToEnd(); logger.Error(string.Format("File uploaded, server response is: {0}", responseStr)); } catch (Exception ex) { logger.Error("Error uploading file", ex); if (wresp != null) { wresp.Close(); wresp = null; } } finally { wr = null; } return responseStr; } #endregion } #endregion #region 枚举 /// <summary> /// 请求方式 /// </summary> public enum Method { GET, POST, DELETE } /// <summary> /// 返回格式 /// </summary> public enum Format { xml, json, } #endregion }