使用XML的VB.NET服务器协议违反“Section = ResponseStatusLine”

时间:2012-06-26 13:09:42

标签: xml vb.net stream xmlhttprequest system.net.webexception

我正在尝试将XML片段发送到我的localhost服务器。我已经能够成功连接并且(我认为)成功发送了代码片段。但是,当我运行我的程序时,我得到一个未处理的WebException。异常详细信息的全文如下:

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 个答案:

答案 0 :(得分: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
    但是,如果这不起作用,请查看其他地方的解决方案,因为我可以不再帮助您。

  4. 希望这有帮助!