由于Web客户端的uploadData不编码数据,那么这将是增加的效果"的Content-Type","的multipart / form-data的"标头, [英] Since WebClient's uploadData doesn't encode data, then what will be the effect of adding a "Content-Type", "multipart/form-data" header to it

查看:163
本文介绍了由于Web客户端的uploadData不编码数据,那么这将是增加的效果"的Content-Type","的multipart / form-data的"标头,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#的 uploadData 的方法不编码正被该数据发送。所以,如果我发送一个文件在使用这种方法(将其转换成字节之后),接收方在寻找一个多种形式/ form-data的后,然后它会显然是不行的。会加入像头:

C#'s uploadData method doesn't encode the data that is being sent. So, if I send a file over (after converting it into bytes) using this method, and the receiving side is looking for a multiform/form-data post, then it will obviously not work. Will adding a header like :

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

请发送加密为各种形式的数据,或者将数据仍然未加密(因而无法解析按预期多种形式的数据服务器)?

make it send the data encrypted as multiform, or will the data be still not encrypted (and hence unparseable by servers expecting multiform data) ?

请注意,我不能使用 Web客户端的 uploadFile ,因为我没有权限来获得对客户端的文件路径位置(我只是有一条小溪,我可以转换为字节)

Note that I can't use WebClient's uploadFile, as I don't have permission to get the file path location on the client side (I just have a stream, that I can convert to bytes)

推荐答案

为什么,如果你希望它是安全的不使用 UploadFile ?使用和将自动加入的multipart / form-data的的照顾。

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

示例 UploadFile

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

还有一件事,编码和加密是2个不同的东西。

And one more thing, encoding and encrypting are 2 different things.

编辑:

您或许应该标记你的问题作为Silverlight的,如果您在使用你的Web客户端项目的WebClient。不管怎么说,在SL WebClient类没有任何UploadData方法。更多信息请参阅本:

You should probably tag your question as Silverlight if you you are using WebClient in your WebClient project. Anyways, WebClient class in SL doesn't have any UploadData method. See this for more info:

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

不管怎么说,这里是工作解决您的问题:

Anyways, here is the working solution to your problem:

在您按一下按钮,有这样的代码:

In your button click, have 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);
                }
            } 

和事件本身:

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为私人字符串_boundaryNo = DateTime.Now.Ticks.ToString(X);

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

我曾将其与Asp.Net MVC 4和Silverlight 5个工作

I had it working with Asp.Net MVC 4 and Silverlight 5.

祝你好运:)

这篇关于由于Web客户端的uploadData不编码数据,那么这将是增加的效果"的Content-Type","的multipart / form-data的"标头,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆