我的WCF REST服务正在返回数据合同列表。当我尝试读取响应时,我得到null,我注意到粗体代码中突出显示的下面的错误。 “length ='requestStream.Length'引发了'System.NotSupportedException'类型的异常”
HttpWebResponse resp;
try
{
if (httpMethod.ToUpper() == Constants.WEBHTTPGET)
{
if (request != null && request.ToString().Length > 0)
url += string.Format("/{0}", request.ToString());
}
// Create HttpWebRequest object
HttpWebRequest WebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
WebRequest.Method = httpMethod;
WebRequest.ContentType = @"application/xml; charset=utf-8";
if (IsTimeoutIncrementRequired(url))
WebRequest.Timeout = (ConfigurationManager.AppSettings["WebRequestTimeout"] != null) ? (Convert.ToInt32(ConfigurationManager.AppSettings["WebRequestTimeout"], CultureInfo.InvariantCulture)) : Constants.WebRequestTimeout;
// Add HTTP headers
WebRequest.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R"));
WebRequest.Headers.Add("x-ms-version", "2009-09-19");
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
if (HttpContext.Current.Session["CurrentUser"] != null)
{
DataContract.User userObj = (DataContract.User)HttpContext.Current.Session["CurrentUser"];
//Set Header content type and Authetincation
WebRequest.Headers.Add("Authorization", GetAuthHeader(userObj.UserName, userObj.Password));
CultureInfo culture = ResolveCulture();
if (culture != null && culture.Name != null)
{
WebRequest.Headers.Add("ClientBrowserCulture", culture.Name);
}
else
{
WebRequest.Headers.Add("ClientBrowserCulture", System.Configuration.ConfigurationManager.AppSettings["DISCurrentCulture"]);
}
}
if (httpMethod.ToUpper() == Constants.WEBHTTPPOST)
{
string xmlString = ObjectToXml(request);
WebRequest.ContentLength = Encoding.UTF8.GetByteCount(xmlString);
**using (Stream stream = WebRequest.GetRequestStream())
{
stream.Write(Encoding.UTF8.GetBytes(xmlString), 0, Encoding.UTF8.GetByteCount(xmlString));
}**
}
else
{
WebRequest.ContentLength = 0;
}
resp = WebRequest.GetResponse() as HttpWebResponse;
}