2015-03-18 33 views
0

我有一個內置小型web服務器的硬件設備。我不控制Web服務器如何工作或編程。它在您請求時發回一個小XML文件,問題是服務器不會發回任何標題信息。從vb.net中的硬件設備讀取遠程XML文件(協議違例)

這是我試圖連接到它的方式。 (我甚至將設備放置在自己的靜態IP上供您測試 - 包含在下面的代碼片段中)。

Dim request As HttpWebRequest = HttpWebRequest.Create("http://96.39.92.3:81/state.xml") 
request.Method = WebRequestMethods.Http.Get 
' I Have added the following two lines to test, they do not help alone or together. 
' I also added: <httpWebRequest useUnsafeHeaderParsing="true" /> to the config file. 
request.ProtocolVersion = HttpVersion.Version10 
request.ServicePoint.Expect100Continue = False 
Dim oResponse As HttpWebResponse = request.GetResponse() 
Dim reader As New StreamReader(oResponse.GetResponseStream()) 
Dim tmp As String = reader.ReadToEnd() 
oResponse.Close() 
If oResponse.StatusCode = "200" Then 
    MsgBox(tmp) 
Else 
    Throw New ApplicationException("Error Occurred, Code: " & oResponse.StatusCode) 
End If  

我也嘗試使用XmlTextReader具有相同的結果。

感謝您的幫助。

編輯:提琴手響應(使用Chrome瀏覽器請求文檔)

RAW REQUEST: 
GET http://96.39.92.3:81/state.xml HTTP/1.1 
Host: 96.39.92.3:81 
Connection: keep-alive 
Cache-Control: max-age=0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-US,en;q=0.8 


RAW RESPONSE: 
HTTP/1.0 200 This buggy server did not return headers 

<?xml version="1.0" encoding="utf-8"?> 
<datavalues> 
<relaystate>1</relaystate> 
<inputstate>0</inputstate> 
<rebootstate>0</rebootstate> 
<totalreboots>0</totalreboots> 
</datavalues> 

使用Fiddler開放,上述作品的HttpWebRequest。 Fiddler必須在返回.net應用程序之前將其添加回來。

編輯2:調整後的標題值爲請求。這使得icepickles tcpclient爲我工作。 Cookie值是必需的。

WriteTextToStream(stream, tcpClient, "GET /state.xml?relayState=1 HTTP/1.0" & ControlChars.CrLf & _ 
" Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*" & ControlChars.CrLf & _ 
"Referer: " & ControlChars.CrLf & "Accept -Language : en -gb" & ControlChars.CrLf & _ 
"Content-Type: application/x-www-form-urlencoded" & ControlChars.CrLf & _ 
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6" & ControlChars.CrLf & _ 
"Host: 192.168.1.232" & ControlChars.CrLf & _ 
"Connection: Keep -Alive" & ControlChars.CrLf & ControlChars.CrLf & _ 
"Cookie:") 
+0

你能按照使用Fiddler顯示響應的要求?或者用瀏覽器檢查它作爲回覆的內容? – Icepickle 2015-03-18 21:47:18

+0

剛剛用提琴手信息編輯問題。 – Justin 2015-03-18 21:54:12

+0

可能是一個愚蠢的問題,但100%確定它是http而不是ftp或其他東西? – 2015-03-18 22:09:35

回答

1

我重新啓動這個帖子的基礎上,意見,原答覆仍然應該在編輯鏈,萬一有人需要/想它。

這個想法是,而不是使用HttpWebRequest,內置所有額外的檢查,您只需使用TcpClient連接到您的服務器或自從您的額外問題,嘗試使用基本套接字與服務器連接,並且手動請求你的數據(並且忽略你沒有得到標題,只是響應中的文件)

以下事情從我的計算機上請求URL,我相信它也可以與以前的TcpClient(見編輯),但在這種情況下,我只是使用了System.Net.Sockets.Socket。

在以前版本的錯誤是實際上是我用C#\ r \ n而不是VB.net VbCrLf在一個字符串文字,我的錯誤;)

Imports System.Net.Sockets 
Imports System.Net 
Imports System.Text 

Module Module1 
    Sub WriteTextToStream(ByRef stream As NetworkStream, text As String) 
     Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(text) 
     stream.Write(buffer, 0, buffer.Length) 
    End Sub 

    Function CreateHeaders(ParamArray headers() As KeyValuePair(Of String, String)) As String 
     Dim message As New StringBuilder() 
     If headers IsNot Nothing Then 
      For Each kvp In headers 
       message.AppendLine(kvp.Key & ": " & kvp.Value) 
      Next 
     End If 
     Return message.ToString() 
    End Function 

    Function DownloadSocketClient(server As String, port As Integer, path As String, host As String) As String 
     Dim hostEntry = Dns.GetHostEntry(server) 
     Dim ipEndPoint As New IPEndPoint(hostEntry.AddressList(0), port) 
     Dim textResponse As String = String.Empty 

     Using tcpClient As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 

      tcpClient.Connect(ipEndPoint) 

      Dim networkHost As String = host & ":" & port 
      Dim headerMessage As String = CreateHeaders(
       New KeyValuePair(Of String, String)("Host", networkHost) 
      ) 

      ' autoconnect 
      Using stream As NetworkStream = New NetworkStream(tcpClient) 
       ' send request to server 
       WriteTextToStream(stream, String.Format("GET /{1} HTTP/1.1{3}{2}{3}", networkHost, path, headerMessage, Environment.NewLine)) 

       'wait till data is available 
       While Not stream.DataAvailable 
        System.Threading.Thread.Sleep(1) 
       End While 

       ' get the response from the networkstream 
       Dim requestSize As Integer = tcpClient.ReceiveBufferSize 
       Dim result As Integer 
       Dim readRequest(requestSize) As Byte 
       While stream.DataAvailable 
        result = stream.Read(readRequest, 0, requestSize) 
        textResponse &= System.Text.Encoding.UTF8.GetString(readRequest, 0, result) 
       End While 
      End Using 
     End Using 
     Return textResponse 
    End Function 

    Sub Main() 
     Dim fileContent As String = DownloadSocketClient("stackoverflow.server.textconnects.com", 81, "state.xml", "stackoverflow.server.textconnects.com") 
     Console.WriteLine("Received file: " & vbCrLf & fileContent) 
     Console.ReadLine() 
    End Sub 

End Module 
+0

我給了這個嘗試,它適用於你寫的越野車服務器,但不是我有的實際設備。我確實爲它生成了一個dns條目,如果你願意看到我的錯誤:http://stackoverflow.server.textconnects.com:81/state.xml Dim fileContent As String = DownloadTcpClient(「96.39。 92.3「,81,」state.xml「,」96.39.92.3「)是我在你的應用程序中用來嘗試我的服務器。 – Justin 2015-03-18 23:47:14

+0

@Justin是的,糾正它,它似乎是從我的工作;) – Icepickle 2015-03-19 01:24:43

+0

感謝這個信息。我更改了請求的標題值,並且所有內容都落到了位置上。顯然COOKIE值是必需的。我將這個添加到我的問題中,如果有人引用它,他們可以看到。再次感謝您的幫助。 – Justin 2015-03-19 02:22:25