问题遇到的现象和发生背景 :
我司之前购买了一台RFID扫描设备,把扫描到的数据上传到云服务器,由于考虑数据隐私需要转到本地。供应商只要调用本地API即可,但是狮子大开口报价需要整个后台系统都需要部署到本地。所以我想修改API云地址,指向本地服务地址,在APIFox测试没有问题的情况下,直接使用扫描客户端POST 本地服务器,报错411,没有所需长度。发现是IIS问题中的Content-Length没有设置!现在在不能修改客户端的情况,有什么方法(或使用哪个Web服务器)绕过Content-Length访问到Web。
查到客户端一些代码,如果是Body是空,httpWebRequest.ContentLength就没有设定:
public static async Task<string> Request(string url, string body)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 5000;
if (body.Length > 0)
{
byte[] btBodys = Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length;
Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(btBodys, 0, btBodys.Length);
requestStream.Close();
}
HttpWebResponse httpWebResponse = (await httpWebRequest.GetResponseAsync()) as HttpWebResponse;
我想要达到的结果:
解决411报错的方法