public virtual async Task <TResponse> GetAsync <TResponse>(string url, HttpRequest request = null, Dictionary <string, string> headersDict = null) { WebRequest webRequest = WebRequest.Create(url); webRequest.Method = "get"; webRequest.ContentType = "application/json"; if (headersDict != null) { foreach (var headerTuple in headersDict) { webRequest.Headers.Add(headerTuple.Key, headerTuple.Value); } } if (request != null) { var jsonData = JsonConvert.SerializeObject(request); byte[] databyte = Encoding.UTF8.GetBytes(jsonData); webRequest.ContentLength = databyte.Length; using (Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(databyte, 0, databyte.Length); } } var response = await webRequest.GetResponseAsync(); using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream ?? throw new InvalidOperationException(), Encoding.GetEncoding("utf-8"))) { string result = reader.ReadToEnd(); var decodeResult = UnicodeHelper.Unicode2String(result); var dataResponse = JsonConvert.DeserializeObject <TResponse>(decodeResult); return(dataResponse); } } }
/// <summary> /// /// </summary> /// <param name="url">访问地址</param> /// <param name="fieldValues">数据字典</param> /// <param name="files">文件列表</param> /// <param name="fileKeys">"uploadimg"、"file0"</param> /// <param name="fileContentType">"image/jpeg"</param> /// <param name="encoding">编码,如UTF8</param> /// <returns></returns> public static string UploadFiles(string url, NameValueCollection fieldValues, string[] files, List <string> fileKeys, string fileContentType, Encoding encoding) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); //1.HttpWebRequest System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.ContentType = $"multipart/form-data; boundary=" + boundary; request.Method = "POST"; request.KeepAlive = true; request.Credentials = CredentialCache.DefaultCredentials; using (Stream stream = request.GetRequestStream()) { //1.1 key/value string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; if (fieldValues != null) { foreach (string key in fieldValues.Keys) { stream.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, fieldValues[key]); byte[] formitembytes = encoding.GetBytes(formitem); stream.Write(formitembytes, 0, formitembytes.Length); } } //1.2 file string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; byte[] buffer = new byte[4096]; int bytesRead = 0; for (int i = 0; i < files.Length; i++) { // stream.Write(boundarybytes, 0, boundarybytes.Length); string header = string.Format(headerTemplate, fileKeys[i], Path.GetFileName(files[i]), fileContentType); byte[] headerbytes = encoding.GetBytes(header); stream.Write(headerbytes, 0, headerbytes.Length); using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read)) { while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { stream.Write(buffer, 0, bytesRead); } } } //1.3 form end stream.Write(endbytes, 0, endbytes.Length); } //2.WebResponse HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader stream = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException())) { var result = stream.ReadToEnd(); var decodeResult = UnicodeHelper.Unicode2String(result); return(decodeResult); } }