Since WebClient uploadData does not encode data, what will be the effect of adding the header "Content-Type", "multipart / form-data"

C # uploadData method does not encode data that is sent. So, if I sent a file (after converting it to bytes) using this method, and the receiving side is looking for a multiform/form-data message, then it obviously will not work. A header will be added like:

 WebClient c = new WebClient(); c.Headers.Add("Content-Type", "multipart/form-data"); 

force it to send data encrypted as diverse, or will the data still not be encrypted (and therefore not verifiable by servers expecting diverse data)?

Note that I cannot use WebClient's uploadFile since I do not have permission to get the location of the file path on the client side (I just have a stream that I can convert to bytes)

+4
source share
1 answer

Why don't you use UploadFile WebClient through https if you want it to be safe? and this will automatically take care of adding multipart/form-data .

Example using UploadFile

http://msdn.microsoft.com/en-us/library/36s52zhs.aspx

And one more thing: encoding and encryption are two different things.

Edit:

You should probably mark your question as Silverlight if you are using WebClient in your WebClient project. In any case, the WebClient class in SL has no UploadData method. See Further Information:

http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.95%29.aspx

In any case, here is a working solution to your problem:

In your button, click this code:

 OpenFileDialog dialog = new OpenFileDialog(); bool? retVal = dialog.ShowDialog(); if (retVal.HasValue && retVal == true) { using (Stream stream = dialog.File.OpenRead()) { MemoryStream memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); WebClient webClient = new WebClient(); webClient.Headers["Content-type"] = "multipart/form-data; boundary=---------------------------" + _boundaryNo; webClient.OpenWriteAsync(new Uri("http://localhost:1463/Home/File", UriKind.Absolute), "POST", new { Stream = memoryStream, FileName = dialog.File.Name }); webClient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webClient_OpenWriteCompleted); } } 

and the event itself:

 void webClient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) { if (e.Error == null) { Stream responseStream = e.Result as Stream; dynamic obj = e.UserState; MemoryStream memoryStream = obj.Stream as MemoryStream; string fileName = obj.FileName; if (responseStream != null && memoryStream != null) { string headerTemplate = string.Format("-----------------------------{0}\r\n", _boundaryNo); memoryStream.Position = 0; byte[] byteArr = memoryStream.ToArray(); string data = headerTemplate + string.Format("Content-Disposition: form-data; name=\"pic\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n", fileName); byte[] header = Encoding.UTF8.GetBytes(data); responseStream.Write(header, 0, header.Length); responseStream.Write(byteArr, 0, byteArr.Length); header = Encoding.UTF8.GetBytes("\r\n"); responseStream.Write(byteArr, 0, byteArr.Length); byte[] trailer = System.Text.Encoding.UTF8.GetBytes(string.Format("-----------------------------{0}--\r\n", _boundaryNo)); responseStream.Write(trailer, 0, trailer.Length); } memoryStream.Close(); responseStream.Close(); } } 

where _boundaryNo private string _boundaryNo = DateTime.Now.Ticks.ToString("x");

I worked with Asp.Net MVC 4 and Silverlight 5.

Good luck :)

+6
source

Source: https://habr.com/ru/post/1438452/


All Articles