0

XML スニペットをローカルホスト サーバーに送信しようとしています。私は正常に接続でき、(私が思うに) スニペットを正常に送信できました。ただし、プログラムを実行すると、未処理の 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

誰かが私のためにこれに光を当てることができますか?

4

1 に答える 1

1

アプリケーションと通信するという意図した目標をまだ達成していませんが、想定していたような XML や HTTP リクエストではなく、生の TCP/IP メッセージを介して通信が行われていることが問題であることがわかりました。そこで、「Server Protocol Violation: Section: 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
    ただし、これでうまくいかない場合は、別の場所で解決策を探してください。

お役に立てれば!

于 2012-06-27T09:11:26.120 に答える