2012-05-15 46 views
0

我得到通過的StreamWriter拋出的錯誤,我得到的消息是:流異常和System.Xml.XmlException:' - '是一個意外的標記。預期令牌是 ''

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上運行此代碼,但它不再工作。

回答

6

最可能的服務器響應不再有效的XML。

使用一些HTTP調試工具(即Fiddler)來查看響應的實際外觀。或者將響應保存爲文本而不是嘗試加載爲XML。

0

一些建議你的代碼的改進:

public XmlDocument SendRequest(string command, string request) 
{ 
    if (!IsInitialized()) 
     return null; 

    var result = new XmlDocument(); 

    try 
    { 
     var prefix = (m_SecureMode) ? "https://" : "http://"; 
     var url = string.Concat(prefix, m_Url, command); 
     var webRequest = (HttpWebRequest) WebRequest.Create(url); 

     webRequest.Method = "POST"; 
     webRequest.ContentType = "text/xml"; 
     webRequest.ServicePoint.Expect100Continue = false; 

     var UsernameAndPassword = string.Concat(m_Username, ":", m_Password); 
     var EncryptedDetails = Convert.ToBase64String(Encoding.ASCII.GetBytes(UsernameAndPassword)); 
     webRequest.Headers.Add("Authorization", "Basic " + EncryptedDetails); 

     using (var requestStream = webRequest.GetRequestStream()) 
     { 
      using (var sw = new StreamWriter(requestStream)) 
      { 
       sw.WriteLine(request); 
      } 
     } 

     using (var webResponse = webRequest.GetResponse()) 
     { 
      using (var responseStream = webResponse.GetResponseStream()) 
      { 
       result.Load(responseStream); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     result.LoadXml("<error></error>"); 
     result.DocumentElement.InnerText = ex.ToString(); 
    } 
    return result; 
} 

您創建一個實現IDisposable應該在using塊中的對象的全部。你省略了WebResponse和結果Stream

此外,XmlDocument.Load可以直接接受Stream,所以沒有必要使用中間StreamReader。 我會進一步建議完全刪除try/catch塊,並且不要強制調用者必須讀取XML以確定是否發生錯誤。但是,如果您堅持這樣做,那麼您至少應該使用XML API來構建您的XML。你不知道ex.ToString()裏面有什麼,所以很有可能把它扔到<error>{0}</error>的中間會產生無效的XML。

相關問題