简体   繁体   English

Webclient标头中的两个“ Content-Type”

[英]two “Content-Type” in webclient header

               var uri = URL_BASE + myuri
                          string.Format("providers/{0}/items?feed={1}&id={2}&type=cf", provider, feed, zipFileNoPath);
                var webClient = new WebClient();
                webClient.Credentials = new NetworkCredential(email, password);
                webClient.Headers.Add("Accept", "*/*");
                webClient.Headers.Add("Content-Type", "application/octet-stream");
                webClient.UploadFileAsync(new Uri(uri), "POST", zipFile);

for the above code, when I watch in from fiddler, I saw two "Content-Type" in header One is Content-Type: multipart/form-data; 对于上面的代码,当我从提琴手观看时,我在标题中看到了两个“ Content-Type”,一个是Content-Type:multipart / form-data; boundary=---------------------8cf27396e080e0a, The other is Content-Type: application/octet-stream why whould this be? boundary = --------------------- 8cf27396e080e0a,另一个是Content-Type:application / octet-stream为什么会是谁? whichone takes effect then, thanks 然后哪个人生效,谢谢

The "boundary" parameter is added when binary data is posted to the server among other values. 在其他值中将二进制数据发布到服务器时,将添加“ boundary”参数。 This is to allow the server to recognize the boundary of the data - the value of the boundary parameter is random and picked so that the string never occurs in the posted data (and thus can server as the boundary). 这是为了使服务器能够识别数据的边界-边界参数的值是随机的并且是经过挑选的,因此字符串永远不会出现在发布的数据中(因此可以作为边界服务器)。

I guess then that the webclient adds this automatically and if this is so, you can comment our your own line which adds this header. 我猜想webclient会自动添加它,如果是这样,您可以在我们自己的添加该标头的行中添加注释。 Unfortunately, I don't know the HTTP specs to tell whether duplicate headers are valid or not. 不幸的是,我不知道HTTP规范可以告诉您重复的标头是否有效。

use UploadData instead of UploadFile 使用UploadData代替UploadFile

            var webClient = new WebClient();
            webClient.Credentials = new NetworkCredential(email, password);                                     
            webClient.UploadDataCompleted += webClient_UploadDataCompleted;
            byte[] fileBytes = File.ReadAllBytes(zipFile);
            webClient.UploadDataAsync(new Uri(uri), "POST", fileBytes);

Then I will only see one Content-Type 那我只会看到一个Content-Type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM