标签:tip end += url 文件上传 path ipa 调用 上传
HTTP工具类,重构封装了常用的3种协议:json、x-www-form-urlencoded、multipart/form-data支持文件上传。
public class Http { public static string Get(string endPoint, Dictionary<string, object> heads = null, int timeout = 15000) { var request = GetRequest(endPoint, heads, timeout); request.Method = "GET"; request.ContentType = "text/plain; charset=UTF-8"; return GetResponseValue(request); } public static string PostJson(string endPoint, string json, Dictionary<string, object> heads = null, int timeout = 15000) { var request = GetRequest(endPoint, heads, timeout); request.ContentType = "application/json; charset=UTF-8"; var bytes = Encoding.UTF8.GetBytes(json); request.ContentLength = bytes.Length; using (var writeStream = request.GetRequestStream()) { writeStream.Write(bytes, 0, bytes.Length); } return GetResponseValue(request); } public static string PostFormUrlEncoded(string endPoint, Dictionary<string, object> data, Dictionary<string, object> heads = null, int timeout = 15000) { var request = GetRequest(endPoint, heads, timeout); request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; string postData = ""; foreach (var item in data) { postData += $"{item.Key}={item.Value}&"; } postData = postData.TrimEnd(‘&‘); var bytes = Encoding.UTF8.GetBytes(postData); request.ContentLength = bytes.Length; using (var writeStream = request.GetRequestStream()) { writeStream.Write(bytes, 0, bytes.Length); } return GetResponseValue(request); } public static string PostFormData(string endPoint, Dictionary<string, object> data, Dictionary<string, object> heads = null, Dictionary<string, KeyValuePair<string, byte[]>> files = null, int timeout = 15000) { var request = GetRequest(endPoint, heads, timeout); var boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("X"); request.ContentType = $"multipart/form-data; charset=UTF-8; boundary={boundary}"; MemoryStream memoryStream = new MemoryStream(); #region 添加普通表单 if (data != null) { // 添加非文件类型的字节流 StringBuilder postData = new StringBuilder(""); foreach (var item in data) { postData.AppendLine($"--{boundary}"); postData.AppendLine($"Content-Disposition: form-data; name=\"{item.Key}\"{Environment.NewLine}"); postData.AppendLine(item.Value + ""); } var bytes = Encoding.UTF8.GetBytes(postData.ToString()); memoryStream.Write(bytes, 0, bytes.Length); } #endregion #region 添加文件表单 if (files != null) { byte[] newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine); // 添加文件字节流 foreach (var file in files) { StringBuilder fileData = new StringBuilder(""); fileData.AppendLine($"--{boundary}"); fileData.AppendLine($"Content-Disposition: form-data; name=\"{file.Key}\"; filename=\"{file.Value.Key}\""); fileData.AppendLine($"Content-Type: application/octet-stream{Environment.NewLine}"); var bytes = Encoding.UTF8.GetBytes(fileData.ToString()); memoryStream.Write(bytes, 0, bytes.Length); memoryStream.Write(file.Value.Value, 0, file.Value.Value.Length); memoryStream.Write(newLineBytes, 0, newLineBytes.Length); } } #endregion #region 添加结尾标记 var endBytes = Encoding.UTF8.GetBytes($"--{boundary}--"); memoryStream.Write(endBytes, 0, endBytes.Length); #endregion var totalBytes = memoryStream.ToArray(); memoryStream.Close(); request.ContentLength = totalBytes.Length; using (var writeStream = request.GetRequestStream()) { writeStream.Write(totalBytes, 0, totalBytes.Length); } return GetResponseValue(request); } protected static HttpWebRequest GetRequest(string endPointWithParams, Dictionary<string, object> heads = null, int timeout = 15000) { var service = new Uri(endPointWithParams); var request = WebRequest.CreateHttp(service); request.Method = "POST"; request.Timeout = timeout; request.ContinueTimeout = timeout; request.KeepAlive = true; request.ContentLength = 0; if (heads != null) { foreach (var head in heads) { if (request.Headers.AllKeys.Contains(head.Key)) request.Headers[head.Key] = head.Value + ""; else request.Headers.Add(head.Key, head.Value + ""); } } return request; } protected static string GetResponseValue(HttpWebRequest request) { using (var response = (HttpWebResponse)request.GetResponse()) { var responseValue = string.Empty; if (response.StatusCode != HttpStatusCode.OK) { var message = string.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode); throw new WebException(message); } using (var responseStream = response.GetResponseStream()) { if (responseStream != null) using (var reader = new StreamReader(responseStream)) { responseValue = reader.ReadToEnd(); } } return responseValue; } } }
调用方式:
var url = "http://localhost:50752/home/test"; var heads = new Dictionary<string, object>(); heads.Add("token", "0000000000000000000000"); var jsonDic = new Dictionary<string, object>(); jsonDic.Add("id", 100); jsonDic.Add("name", "上海市"); jsonDic.Add("level", 987.03); var jsonResult = Http.PostJson(url, jsonDic.ToJson(), heads); var postFormUrlEncoded = Http.PostFormUrlEncoded(url, jsonDic, heads); var filepath = "F:\\008-Word\\2017.10.30.pdf"; var bytes = File.ReadAllBytes(filepath); var fileInfo = new FileInfo(filepath); var files = new Dictionary<string, KeyValuePair<string, byte[]>>(); files.Add("fil", new KeyValuePair<string, byte[]>(fileInfo.Name, bytes)); files.Add("ni", new KeyValuePair<string, byte[]>(fileInfo.Name, bytes)); var postFormData = Http.PostFormData(url, jsonDic, heads, files); Console.ReadLine();
标签:tip end += url 文件上传 path ipa 调用 上传
原文地址:https://www.cnblogs.com/jonney-wang/p/12180159.html