Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private HttpWebResponse MakeFormDataRequest(Dictionary<string, string> files, Dictionary<string, string> otherValues, string contentType = "", NameValueCollection headers = null)
- {
- var req = (HttpWebRequest)WebRequest.Create(this.Url);
- req.Timeout = 10000 * 1000;
- req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
- req.AllowAutoRedirect = false;
- req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
- var mimeParts = new List<MimePart>();
- try
- {
- if (headers != null)
- {
- req.Headers.Add(headers);
- }
- if (otherValues != null)
- {
- foreach (var fieldName in otherValues.Keys)
- {
- var part = new MimePart();
- part.Headers["Content-Disposition"] = "form-data; name=\"" + fieldName + "\"";
- part.Data = new MemoryStream(Encoding.UTF8.GetBytes(otherValues[fieldName]));
- mimeParts.Add(part);
- }
- }
- if (files != null)
- {
- foreach (var fieldName in files.Keys)
- {
- var part = new MimePart();
- part.Headers["Content-Disposition"] = "form-data; name=\"" + fieldName + "\"; filename=\"" + Path.GetFileName(files[fieldName]) + "\"";
- part.Data = System.IO.File.OpenRead(files[fieldName]);
- var mimePartProvider = new FileExtensionContentTypeProvider();
- var defaultContentType = "application/octet-stream";
- if (!mimePartProvider.TryGetContentType(files[fieldName], out string fileContentType)) {
- fileContentType = defaultContentType;
- }
- part.Headers["Content-Type"] = fileContentType;
- mimeParts.Add(part);
- }
- }
- string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
- req.ContentType = !String.IsNullOrEmpty(contentType) ? contentType : "multipart/form-data; boundary=" + boundary;
- req.Method = this.Method;
- long contentLength = 0;
- byte[] _footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
- foreach (MimePart part in mimeParts)
- {
- contentLength += part.GenerateHeaderFooterData(boundary);
- }
- req.ContentLength = contentLength + _footer.Length;
- byte[] buffer = new byte[8192];
- byte[] afterFile = Encoding.UTF8.GetBytes("\r\n");
- int read;
- using (Stream s = req.GetRequestStream())
- {
- foreach (MimePart part in mimeParts)
- {
- s.Write(part.Header, 0, part.Header.Length);
- while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
- s.Write(buffer, 0, read);
- part.Data.Dispose();
- s.Write(afterFile, 0, afterFile.Length);
- }
- s.Write(_footer, 0, _footer.Length);
- }
- var res = (HttpWebResponse)req.GetResponse();
- return res;
- }
- catch (Exception ex)
- {
- if (ex is WebException) {
- var response = ((WebException)ex).Response?.GetResponseStream();
- if (ex.Message.Contains("302")) {
- return (HttpWebResponse)((WebException)ex).Response;
- }
- if (response != null) {
- using (var stream = new StreamReader(response)) {
- var error = stream.ReadToEnd();
- if (!string.IsNullOrEmpty(error)) {
- throw new Exception(error);
- }
- }
- }
- }
- Console.WriteLine(ex.Message);
- foreach (MimePart part in mimeParts)
- if (part.Data != null)
- part.Data.Dispose();
- return (HttpWebResponse)req.GetResponse();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement