我有一個內置小型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:")
你能按照使用Fiddler顯示響應的要求?或者用瀏覽器檢查它作爲回覆的內容? – Icepickle 2015-03-18 21:47:18
剛剛用提琴手信息編輯問題。 – Justin 2015-03-18 21:54:12
可能是一個愚蠢的問題,但100%確定它是http而不是ftp或其他東西? – 2015-03-18 22:09:35