/// <summary> 设置请求体 </summary> /// <param name="request"></param> /// <param name="param"> </param> public static void SetHttpWebRequestBodyContent(HttpWebRequest request, HttpTaskParams param) { if (request == null || param == null) { return; } if (param.Method == HttpTaskParams.METHOD_GET) { //nothing,get 请求没有请求体,也不允许写请求体? } else { //POST 或 其他类型,优先写自定义请求体 if (string.IsNullOrEmpty(param.CustomStringContentBody)) { if (param.getFileParams() == null) { //写post请求体 SetHttpWebRequestStringBodyContent(request, GetStringByPairs(param.getStringParams(), param.IsAutoEncoding())); } else { //写上传请求体 SetHttpWebRequestUploadBodyContent(request, param); } } else { //写自定义请求体 SetHttpWebRequestStringBodyContent(request, param.CustomStringContentBody); } } }
/// <summary> 设置上传请求体 </summary> /// <param name="request"></param> /// <param name="param"> </param> private static void SetHttpWebRequestUploadBodyContent(HttpWebRequest request, HttpTaskParams param) { if (request == null || param == null) { return; } // 1.构造分界线标识 string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")); // 分界线可以自定义参数 byte[] boundaryBytes = Encoding.UTF8.GetBytes(string.Format("\r\n--{0}\r\n", boundary)); //2.上传header属性设置 request.Method = "POST"; request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); byte[] bytes = null; Stream stream = request.GetRequestStream(); //4.写 key value 数据 List <NameValuePair> pairs = param == null ? null : param.getStringParams(); if (pairs != null && pairs.Count > 0) { string pairStr; NameValuePair pair; for (int i = 0; i < pairs.Count; i++) { //写分割线 stream.Write(boundaryBytes, 0, boundaryBytes.Length); //写key value pair = pairs[i]; pairStr = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", pair.Name, pair.Value); bytes = Encoding.UTF8.GetBytes(pairStr); stream.Write(bytes, 0, bytes.Length); } } //5.写上传文件数据 List <NameFilePair> filePairs = param == null ? null : param.getFileParams(); if (filePairs != null && filePairs.Count > 0) { NameFilePair filePair; string pairStr; string fileName; string contentType; byte[] buffer = new byte[1024 * 100]; int bytesRead = 0; for (int i = 0; i < filePairs.Count; i++) { //写分割线 stream.Write(boundaryBytes, 0, boundaryBytes.Length); //写文件属性 filePair = filePairs[i]; fileName = string.IsNullOrEmpty(filePair.FileName) ? new FileInfo(filePair.FilePath).Name : filePair.FileName; contentType = string.IsNullOrEmpty(filePair.ContentType) ? "application/octet-stream" : filePair.ContentType; pairStr = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", filePair.Name, fileName, contentType); bytes = Encoding.UTF8.GetBytes(pairStr); stream.Write(bytes, 0, bytes.Length); //写文件数据 using (FileStream fileStream = new FileStream(filePair.FilePath, FileMode.Open, FileAccess.Read)) { while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { stream.Write(buffer, 0, bytesRead); } } } } // 6.写结束分界线 bytes = Encoding.UTF8.GetBytes(string.Format("\r\n--{0}--\r\n", boundary)); stream.Write(bytes, 0, bytes.Length); //7.关闭请求流 stream.Close(); }