简体   繁体   中英

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; boundary=---------------------8cf27396e080e0a, The other is Content-Type: application/octet-stream why whould this be? whichone takes effect then, thanks

The "boundary" parameter is added when binary data is posted to the server among other values. 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. Unfortunately, I don't know the HTTP specs to tell whether duplicate headers are valid or not.

use UploadData instead of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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