StreamWriter によってスローされたエラーが発生しています。取得しているメッセージは次のとおりです。
Length = '(sw.BaseStream).Length' threw an exception of type 'System.NotSupportedException'
Position = '(sw.BaseStream).Position' threw an exception of type 'System.NotSupportedException'
スタック トレース:
Message: System.Xml.XmlException: '6163592' is an unexpected token. The expected token is '\'" or "".
Stack Trace: System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
at System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(String expectedToken1, String expectedToken2)
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(Stream inStream)
at BasecampManager.SendRequest(String command, String request) in C:\Inetpub\wwwroot\basecamp\Basecamp_Net_20_API_src\BasecampAPI\BasecampManager.cs:line 146
私のコード:
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);
//MessageBox.Show(webRequest.GetRequestStream().ToString());
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;
}
何が問題なのかわかりません。多くの投稿をチェックしましたが、何も役に立ちません。
このコードは、数か月前に完全に機能していました。これで、例外により自動的に動作が停止しました。VS 2005 に何らかの問題があるのではないかと考えて、VS 2005、2008、2010 でこのコードを実行しようとしましたが、機能しなくなりました。