C# 发送Web服务请求时出错:必须提供请求正文
我已经写了下面的代码-C# 发送Web服务请求时出错:必须提供请求正文,c#,asp.net,.net,asp.net-mvc,web-services,C#,Asp.net,.net,Asp.net Mvc,Web Services,我已经写了下面的代码- HttpWebRequest lHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); lHttpWebRequest.Method = "POST"; lHttpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate"); lHttpWebRequest.Con
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();
我补充说-
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();
}
这起作用了