发送 Web 服务请求时出错:必须提供请求正文
Posted
技术标签:
【中文标题】发送 Web 服务请求时出错:必须提供请求正文【英文标题】:Error in sending Web Service Request : Must provide request body 【发布时间】:2020-12-31 21:44:06 【问题描述】:我已经写了下面的代码-
HttpWebRequest lHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
lHttpWebRequest.Method = "POST";
lHttpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
lHttpWebRequest.ContentType = "text/xml";
lHttpWebRequest.KeepAlive = false;
ASCIIEncoding lEnCoding = new ASCIIEncoding();
byte[] lData = lEnCoding.GetBytes(xmlDocument.InnerXml);
lHttpWebRequest.ContentLength = lData.Length;
HttpWebResponse lHttpWebResponse = (HttpWebResponse)lHttpWebRequest.GetResponse();
我收到错误 -
You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse.
我在最后一行遇到错误 -
HttpWebResponse lHttpWebResponse = (HttpWebResponse)lHttpWebRequest.GetResponse();
【问题讨论】:
***.com/questions/4256136/… 【参考方案1】:我添加了 -
ASCIIEncoding lEnCoding = new ASCIIEncoding();
byte[] lData = lEnCoding.GetBytes(xmlDocument.InnerXml);
lHttpWebRequest.ContentLength = lData.Length;
using (Stream requestStream = lHttpWebRequest.GetRequestStream())
requestStream.Write(lData, 0, lData.Length);
requestStream.Close();
这行得通。
【讨论】:
以上是关于发送 Web 服务请求时出错:必须提供请求正文的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 .net core web api 上运行 ASE sql (sybase) 时出错“不允许发送或接收数据的请求......”