我正在尝试使用 http post 将一些数据从 asp.net 发布到 web 服务。
这样做时,我收到了附带的错误。我检查了很多帖子,但没有什么能真正帮助我。对此的任何帮助将不胜感激。
Length = 'dataStream.Length' 引发了“System.NotSupportedException”类型的异常
Position = 'dataStream.Position' 引发了“System.NotSupportedException”类型的异常
请附上我的代码:
public XmlDocument SendRequest(string command, string request)
{
XmlDocument result = null;
if (IsInitialized())
{
result = new XmlDocument();
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
try
{
string prefix = (m_SecureMode) ? "https://" : "http://";
string url = string.Concat(prefix, m_Url, command);
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml";
webRequest.ServicePoint.Expect100Continue = false;
string UsernameAndPassword = string.Concat(m_Username, ":", m_Password);
string EncryptedDetails = Convert.ToBase64String(Encoding.ASCII.GetBytes(UsernameAndPassword));
webRequest.Headers.Add("Authorization", "Basic " + EncryptedDetails);
using (StreamWriter sw = new StreamWriter(webRequest.GetRequestStream()))
{
sw.WriteLine(request);
}
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
webResponse = (HttpWebResponse)webRequest.GetResponse();
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
{
result.Load(sr.BaseStream);
sr.Close();
}
}
catch (Exception ex)
{
string ErrorXml = string.Format("<error>{0}</error>", ex.ToString());
result.LoadXml(ErrorXml);
}
finally
{
if (webRequest != null)
webRequest.GetRequestStream().Close();
if (webResponse != null)
webResponse.GetResponseStream().Close();
}
}
return result;
}
提前致谢 !!
拉蒂卡