I am working on a c# project. I have to call an external web service and post file as binary data and read its response. Below is my code
var webClient = new WebClient();
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
var fileData = webClient.Encoding.GetString(file);
var package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, filename, contentType, fileData);
var nfile = webClient.Encoding.GetBytes(package);
byte[] resp = webClient.UploadData(url, "POST", nfile);
I have passed content type as "application/octet-stream". I am surely missing something but cannot figure it out.
HttpClientrather thanWebClient. It'll handle the fiddly bits for you.WebClient.UploadFile? See MSDNUploadFilein place ofUploadData