我有以下代码将数据PUT到api:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ConfigUtil.ApiEndPoint);
req.ContentType = "application/json";
req.Headers.Add(ConfigUtil.GetUserAdminApiHeader());
req.Method = "PUT";
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(jsonData);
req.ContentLength = data.Length;
using (System.IO.Stream newStream = req.GetRequestStream())
{
newStream.Write(data, 0, data.Length);
WebResponse response = req.GetResponse();
status =response!=null? ((HttpWebResponse)response).StatusCode:HttpStatusCode.InternalServerError;
}
当我在本地运行时,它运行完美,但是当我在生产中运行相同的应用程序时,我收到错误
'System.Net.ProtocolViolationException'{“您必须编写ContentLength 调用[Begin] GetResponse之前到请求流的字节。“}
如上面的代码所示,我将contentlength字节写入请求流。为什么这会在生产中失败(Windows 2012 R2)而不是在本地?