由于WebClient的uploadData不对数据进行编码,因此向其添加“Content-Type”,“multipart / form-data”标头会产生什么影响?

时间:2012-10-08 16:10:12

标签: c# http multipartform-data

C#的uploadData方法不对正在发送的数据进行编码。因此,如果我使用此方法发送文件(将其转换为字节后),并且接收方正在寻找multiform/form-data帖子,那么它显然不起作用。将添加如下标题:

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

让它发送加密为multiform的数据,或者数据是否仍未加密(因此服务器无法解析预期的多种数据)?

请注意,我无法使用WebClient's uploadFile,因为我没有权限在客户端获取文件路径位置(我只有一个流,我可以转换为字节)

1 个答案:

答案 0 :(得分:6)

如果您想让它安全,为什么不使用WebClient UploadFile而不是https?这将自动负责添加multipart/form-data

使用UploadFile

的示例

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

还有一件事,编码和加密是两回事。

修改

如果您在WebClient项目中使用WebClient,则应该将问题标记为Silverlight。无论如何,SL中的WebClient类没有任何UploadData方法。有关详细信息,请参阅此处:

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

无论如何,这是解决问题的有效方法:

在按钮单击中,请输入以下代码:

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);
                }
            } 

和事件本身:

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();
            }
        }

其中_boundaryNo是private string _boundaryNo = DateTime.Now.Ticks.ToString("x");

我使用过Asp.Net MVC 4和Silverlight 5。

祝你好运:)