後臺使用multipart/form-data方式提交資料
阿新 • • 發佈:2018-12-12
protected string CreateFormDateResponse(string url, Encoding encoding, IDictionary<string, string> textParams, IDictionary<string, FileinFo> fileParams) { try { HttpWebRequest request; // 重新設定請求頭 request = (HttpWebRequest)WebRequest.Create(url); string host = url.Replace("http://", "").Replace("https://", ""); host = host.Remove(host.IndexOf('/')); request.Host = host; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8;"; request.KeepAlive = true; request.Referer = url; request.ProtocolVersion = HttpVersion.Version11; request.Method = "POST"; string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線 request.ContentType = "multipart/form-data;charset=gb2312;boundary=" + boundary; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; LCTE; rv:11.0) like Gecko"; request.Timeout = 5000; request.Headers["Upgrade-Insecure-Requests"] = "1"; request.Headers["Accept-Encoding"] = "gzip, deflate"; request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9"; request.Headers["Cache-Control"] = "no-cache"; request.Headers["Pragma"] = "no-cache"; // 自動追蹤30X跳轉 request.AllowAutoRedirect = true; byte[] itemBoundaryBytes = encoding.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endBoundaryBytes = encoding.GetBytes("\r\n--" + boundary + "--\r\n"); // 組裝文字請求引數 string textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}"; IEnumerator<KeyValuePair<string, string>> textEnum = textParams.GetEnumerator(); using (System.IO.Stream reqStream = request.GetRequestStream()) { while (textEnum.MoveNext()) { string textEntry = string.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value); byte[] itemBytes = encoding.GetBytes(textEntry); reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); reqStream.Write(itemBytes, 0, itemBytes.Length); } // 組裝檔案請求引數 string fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n"; IEnumerator<KeyValuePair<string, FileinFo>> fileEnum = fileParams.GetEnumerator(); while (fileEnum.MoveNext()) { string key = fileEnum.Current.Key; FileinFo fileItem = fileEnum.Current.Value; string fileEntry = string.Format(fileTemplate, key, fileItem.filename, fileItem.ContentType); byte[] itemBytes = encoding.GetBytes(fileEntry); reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); reqStream.Write(itemBytes, 0, itemBytes.Length); byte[] fileBytes = fileItem.data; reqStream.Write(fileBytes, 0, fileBytes.Length); } reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); reqStream.Close(); } ServicePointManager.DefaultConnectionLimit = 200; using (HttpWebResponse rsp = (HttpWebResponse)request.GetResponse()) { try { // 以字元流的方式讀取HTTP響應 using (Stream stream = rsp.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, encoding)) { return reader.ReadToEnd(); } } } catch (Exception ex) { LogInfo.Error("響應文字錯誤", ex); return null; } finally { // 釋放資源 if (rsp != null) rsp.Close(); } } } catch (WebException e) { LogInfo.Error("WebException異常url=" + url + ";\r\ndatabuffer=", e); return string.Empty; } catch (Exception e) { LogInfo.Error("異常url=" + url, e); return string.Empty; } } public class FileinFo { public string name { set; get; } public string filename { set; get; } public string ContentType { set; get; } public byte[] data { set; get; } }