toosensitive
toosensitive

Reputation: 2375

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

Upvotes: 0

Views: 3124

Answers (2)

toosensitive
toosensitive

Reputation: 2375

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

Upvotes: 1

Wiktor Zychla
Wiktor Zychla

Reputation: 48279

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.

Upvotes: 0

Related Questions