2012-06-26 112 views
0

我試圖發送一個XML片段到我的本地主機服務器。我已經能夠成功連接和(我認爲)成功發送片段。但是,當我運行我的程序時,我得到一個未處理的WebException。異常詳細信息全文如下:使用XML的VB.NET服務器協議違規「Section = ResponseStatusLine」

System.Net.WebException was unhandled 
Message=The server committed a protocol violation. Section=ResponseStatusLine 
Source=System 
StackTrace: 
    at System.Net.HttpWebRequest.GetResponse() 
    at Automation_Algorithm.AutomationForm.cmdStart_Click(Object sender, EventArgs e) in C:\Users\ConzM\documents\visual studio 2010\Projects\Automation Algorithm\Automation Algorithm\AutomationForm.vb:line 29 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 
    at Automation_Algorithm.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 

我的代碼(在第11場首發):

Private Sub cmdStart_Click(sender As System.Object, e As System.EventArgs) Handles cmdStart.Click 
    Dim requestNF As WebRequest = WebRequest.Create("http://127.0.0.1:4096") 
    requestNF.Method = "POST" 
    Dim datastring As String 
    Dim getdata = 
     <?xml version='1.0' encoding='ISO-8859-1'?> 
     <MLCommandSet> 
      <info/> 
     </MLCommandSet>                  '/ 
    datastring = "<?xml version='1.0' encoding='ISO-8859-1'?>" & vbNewLine & getdata.ToString() 
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(datastring) 
    requestNF.ContentLength = byteArray.Length 
    requestNF.ContentType = "text/xml" 
    Dim dataStream As Stream = requestNF.GetRequestStream() 
    dataStream.Write(byteArray, 0, byteArray.Length) 
    txtXMLOutFF.AppendText(getdata.ToString & vbNewLine) 
    dataStream.Close() 

    Dim responseNF As Object = requestNF.GetResponse.GetResponseStream '<---breaks here' 
    Console.WriteLine(CType(responseNF, HttpWebResponse).StatusDescription.ToString) 
    txtXMLInFF.Text = CType(responseNF, HttpWebResponse).StatusDescription.ToString 
    dataStream = responseNF.GetResponseStream 
    Dim readerNF As New StreamReader(dataStream) 
    Dim responseFromServerNF As String = readerNF.ReadToEnd 
    Console.WriteLine(responseFromServerNF) 
    txtXMLInFF.AppendText(responseFromServerNF.ToString & vbNewLine) 
    readerNF.Close() 
    dataStream.Close() 
    responseNF.Close() 
End Sub 

有人能提供一些線索這對我來說?

回答

1

儘管我還沒有達到與應用程序通信的預期目標,但我確實發現問題是通信是通過原始TCP/IP消息,而不是像我所假設的XML或HTTP請求。所以,我想出了一個解決疑難清單運行時,你得到的「服務器違反協議:條:ResponseStatusLine」錯誤:

  1. 下面的代碼添加到您的app.config文件:(它允許不正確頭部可以通過)

    <system.net> 
        <settings> 
         <httpWebRequest useUnsafeHeaderParsing = "true"/> 
        </settings> 
    </system.net> 
    
  2. 如果在清理和重建後不起作用,那麼您連接的服務器可能不會發送WebRequest數據。爲了測試這個,我使用了一個Telnet客戶端。
  3. 要使用Telnet測試數據,請下載並打開Telnet客戶端(Windows,我建議PuTTYtel,Unix和其他人通常可以啓動他們的終端/命令提示符並鍵入telnet來激活)。

    a)輸入您嘗試連接的服務器和相應字段中的端口號(Windows)或在終端中鍵入telnet -o servername:portnumber,然後按Enter鍵。 (Unix/Linux風格)

    b)當窗口出現時,嘗試輸入原始查詢,仔細記錄任何空白或換行規範。在我的情況下,我鍵入:

    <?xml version='1.0' encoding='ISO-8859-1'?> 
    <MLCommandSet> 
        <info/> 
    </MLCommandSet> 
    

    並按下回車鍵。當我這樣做時,我立即看到了我正在尋找的應用程序的輸出。 :D
    但是,如果這不起作用,請在其他地方尋找解決方案,因爲我可以幫您解決問題。

希望這有助於!

相關問題