At one time I wrote an article .Net in 1C. Asynchronous HTTP requests, Post sending several multipart / form-data files, compressing traffic using gzip, deflate, convenient parsing of sites, etc.
True there is an example on 1C but it is not particularly important.
public async Task SendPostFile(string urlserviceapiSent) { using (var client = new System.Net.Http.HttpClient()) using (var content = new System.Net.Http.MultipartFormDataContent()) { client.BaseAddress = new System.Uri(urlserviceapiSent); var values = new System.Collections.Generic.Dictionary<String, String>() { { "Name", "name"}, { "id", "id"} }; // content.Add(new FormUrlEncodedContent(values)); foreach (var keyValuePair in values) { content.Add(new System.Net.Http.StringContent(keyValuePair.Value), keyValuePair.Key); } string fileName = @"C:/ТестXML"; var fileContent = new System.Net.Http.StreamContent(System.IO.File.OpenRead(fileName)); fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = System.IO.Path.GetFileName(fileName) }; content.Add(fileContent); // Вариант отправки двоичных данных из файла но более краткий var fileContent2 = new System.Net.Http.StreamContent(System.IO.File.OpenRead(fileName)); content.Add(fileContent2, "attachment", "TestXml"); var stringContent = new System.Net.Http.ByteArrayContent(Encoding.UTF8.GetBytes("Тестовая строка")); stringContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = "Строка" }; content.Add(stringContent); var requestUri = "api/values/SendFiles"; var result = await client.PostAsync(requestUri, content); var str = await result.Content.ReadAsStringAsync(); textBox.AppendText(str + Environment.NewLine); } }
Gives out such
// This is what the sent request looks like // POST http: // localhost: 40320 / api / values / SendFiles HTTP / 1.1 // Content-Type: multipart / form-data; boundary = "9f2d525a-7383-46ab-8fc7-419d73486c02" // Host: localhost: 40320 // Content-Length: 811 // Expect: 100-continue // Connection: Keep-Alive
// - 9f2d525a-7383-46ab-8fc7-419d73486c02 // Content-Type: text / plain; charset = utf-8 // Content-Disposition: form-data; name = Name
// name // - 9f2d525a-7383-46ab-8fc7-419d73486c02 // Content-Type: text / plain; charset = utf-8 // Content-Disposition: form-data; name = id
// id // - 9f2d525a-7383-46ab-8fc7-419d73486c02 // Content-Disposition: form-data; filename = "=? utf-8? B? 0J / RgNC + 0YHRgtC + 0KHRgtGA0L7QutCw? ="; name = attachment // Content-Type: text / plain
// Test line // - 9f2d525a-7383-46ab-8fc7-419d73486c02 // Content-Disposition: form-data; filename = "=? utf-8? B? 0KLQtdGB0YJYTUw =? =" // Content-Type: application / octet-stream
// 12345 // - 9f2d525a-7383-46ab-8fc7-419d73486c02 // Content-Disposition: form-data; name = attachment; filename = TestXml; filename * = utf-8''TestXml
// 12345 // - 9f2d525a-7383-46ab-8fc7-419d73486c02--
There is also a link to the service http://pastebin.com/1kyhAdai
[NonAction] string fileName(MultipartFileData fileData) { string fileName = fileData.Headers.ContentDisposition.FileName; if (string.IsNullOrEmpty(fileName)) { return ""; } if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } return fileName; } [HttpPost] public async Task<HttpResponseMessage> SendFiles() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); try { StringBuilder sb = new StringBuilder(); // Holds the response body // Read the form data and return an async task. await Request.Content.ReadAsMultipartAsync(provider); // This illustrates how to get the form data. foreach (var key in provider.FormData.AllKeys) { foreach (var val in provider.FormData.GetValues(key)) { sb.Append(string.Format("{0}: {1}\n", key, val)); } } // This illustrates how to get the file names for uploaded files. foreach (var file in provider.FileData) { var fileInfo = new FileInfo(file.LocalFileName); sb.Append(string.Format("Uploaded file: {0} {1} ({2} bytes)\n", fileName(file), fileInfo.Name, fileInfo.Length)); } return new HttpResponseMessage() { Content = new StringContent(sb.ToString()) }; } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } }
[HttpPost]-method data is coming? What do you mean by “second step”? - VladD