我想使用 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();
我不知道它为什么会失败,如果我包含“dataStream.Write(fileStream, 0, fileStream.Length);” 线服务器无法接受我的流,说有一个内部错误。我有一个有效的 CURL 命令向我显示服务器想要的数据。
curl -k -X POST -u name:pw -H "Content-type: application/json"
-H 'Accept:application/json''url'
-d'{parameters}'
--data-binary @file.wav
如果我排除了上述行,该命令按预期工作,告诉我没有输入文件。所以我认为file.wav可能有问题