I want to send data and a file using WebRequest.
byte[] fileStream = File.ReadAllBytes(path);
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
WebRequest request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
request.Credentials = new NetworkCredential("name", "pw");
Stream dataStream = request.GetRequestStream();
dataStream.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length);
//dataStream.Write(fileStream, 0, fileStream.Length);
dataStream.Write(fileStream, 0, fileStream.Length); //CAUSES A CRASH
dataStream.Close();
WebResponse response = request.GetResponse();
I don't know why it fails, if I include the "dataStream.Write(fileStream, 0, fileStream.Length);" line the server fails to accept my stream, saying there was an internal error. I have an working CURL command showing me the data the Server wants.
curl -k -X POST -u name:pw -H "Content-type: application/json"
-H 'Accept:application/json''url'
-d'{parameters}'
--data-binary @file.wav
In case I exclude the mentioned line, the command works as expected, telling me that there was no input file. So I think there might be something wrong about the file.wav