2011-06-28 6 views
2

なぜでしょうか。req.GetRequestStream()。Close();原因 "ProtocolViolationException - この動詞タイプのコンテンツ本体を送信できません。"コードスニペットはhereです。ありがとう。ASP.NET ProtocolViolationException - この動詞タイプのコンテンツ本体を送信できません。

 WebRequest req = null; 
     WebResponse rsp = null; 
     try 
     { 
      string fileName = "Login.xml"; 
      string uri = "http://localhost/api/login"; 
      req = WebRequest.Create(uri); 

      //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy 
      req.Method = "POST";  // Post method 
      req.ContentType = "text/xml";  // content type 

      // Wrap the request stream with a text-based writer 
      StreamWriter writer = new StreamWriter(req.GetRequestStream()); 

      // Write the XML text into the stream 
      writer.WriteLine(this.GetTextFromXMLFile(fileName)); 
      writer.Close(); 

      // Send the data to the webserver 
      rsp = req.GetResponse(); 

     } 
     catch (WebException webEx) 
     { 
      LOG.Error(webEx.StackTrace.ToString()); 
     } 
     catch (Exception ex) 
     { 
      LOG.Error(ex.StackTrace.ToString()); 
     } 
     finally 
     { 
      if (req != null) req.GetRequestStream().Close(); 
      if (rsp != null) rsp.GetResponseStream().Close(); 
     } 
+0

これはASP.NETとどのように関連していますか? – dtb

答えて

2

あなたはreq.ContentType = application/xml代わりのtext/xmlを使用してみましたか?

+0

私は同様の問題があり、 'req.ContentType = application/xml'で解決できません。 – Robert

関連する問題