既然WebClient的uploadData不对数据进行编码,那么添加一个"Content-Type","multipart/form-data"标头会有什么效果。



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

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

让它发送加密为多格式的数据,还是数据仍然没有加密(因此无法被期望多形式数据的服务器解析)?

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

如果您希望WebClient安全,为什么不使用UploadFile over 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}rn", _boundaryNo);
                    memoryStream.Position = 0;
                    byte[] byteArr = memoryStream.ToArray();
                    string data = headerTemplate + string.Format("Content-Disposition: form-data; name="pic"; filename="{0}"rnContent-Type: application/octet-streamrnrn", fileName);
                    byte[] header = Encoding.UTF8.GetBytes(data);
                    responseStream.Write(header, 0, header.Length);
                    responseStream.Write(byteArr, 0, byteArr.Length);
                    header = Encoding.UTF8.GetBytes("rn");
                    responseStream.Write(byteArr, 0, byteArr.Length);
                    byte[] trailer = System.Text.Encoding.UTF8.GetBytes(string.Format("-----------------------------{0}--rn", _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一起工作。

祝你好运:)

相关内容

最新更新