E
Ed
Background: I'm attempting to access a web page hosted on a server
embedded in firmware on a device. I initially tried using the
HttpWebRequest (which I know is coded properly since it works with
other web servers) but the server embedded in the firmware is
returning an error and the reponse object is empty. This is curious to
me as the same page returns without errors when viewed in a browser.
The error is:
System.Net.WebException was caught
Message="The server committed a protocol violation.
Section=ResponseStatusLine"
Source="System"
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at WWIP.frmMain.PollRouter()
ServerProtocolViolation {11}
I would like to attempt to perform the same task at a lower level to
see exactly what it being returned from the server in order to
diagnose the problem and work around it. I'm attempting to use the
TcpClient class and a slightly modified version of the sample code in
the MSDN documentation for this class:
http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcpclient(vs.80).aspx
What do I need to "write" to the NetworkStream to initiate a response
from the server? I'm guessing some format of an HTTP Request, but I'm
not sure the format of if that's even correct? Currently, I'm testing
with my local IIS and the NetworkStream.DataAvailable property is
always false, so it appears that nothing is being received from the
server on port 80.
There are pleny of examples of how to code my own TcpListener to talk
with a TcpClient, but I don't see any examples of how to get the
TcpClient to talk with a HTTP or FTP server.
-----
Private Sub btnTestPort80_Click( ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnTestPort80.Click
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim server As String
Dim message As String
server = "localhost"
message = "GET http://localhost/default.asp HTTP/1.1"
Dim port As Int32 = 80
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte
array.
Dim data As [Byte]() =
System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)
txtResults.AppendText(Environment.NewLine & "Sent: " &
message.ToString)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
if not stream.DataAvailable then
dim x as Int32
x = 1
do
''stream = client.GetStream()
x = x + 1
if x > 100 then exit do
loop until stream.DataAvailable
end if
if stream.DataAvailable then
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0,
bytes)
end if
Console.WriteLine("Received: {0}", responseData)
txtResults.AppendText(Environment.NewLine & "Received: " &
responseData.ToString)
' Close everything.
stream.Close()
client.Close()
Catch ex As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", ex)
txtResults.AppendText(Environment.NewLine &
"ArgumentNullException: " & ex.ToString)
Catch ex As SocketException
Console.WriteLine("SocketException: {0}", ex)
txtResults.AppendText(Environment.NewLine & "SocketException: "
& ex.ToString)
Catch ex As Exception
txtResults.AppendText(Environment.NewLine & "Exception: " &
ex.ToString)
End Try
End Sub
-----
embedded in firmware on a device. I initially tried using the
HttpWebRequest (which I know is coded properly since it works with
other web servers) but the server embedded in the firmware is
returning an error and the reponse object is empty. This is curious to
me as the same page returns without errors when viewed in a browser.
The error is:
System.Net.WebException was caught
Message="The server committed a protocol violation.
Section=ResponseStatusLine"
Source="System"
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at WWIP.frmMain.PollRouter()
ServerProtocolViolation {11}
I would like to attempt to perform the same task at a lower level to
see exactly what it being returned from the server in order to
diagnose the problem and work around it. I'm attempting to use the
TcpClient class and a slightly modified version of the sample code in
the MSDN documentation for this class:
http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcpclient(vs.80).aspx
What do I need to "write" to the NetworkStream to initiate a response
from the server? I'm guessing some format of an HTTP Request, but I'm
not sure the format of if that's even correct? Currently, I'm testing
with my local IIS and the NetworkStream.DataAvailable property is
always false, so it appears that nothing is being received from the
server on port 80.
There are pleny of examples of how to code my own TcpListener to talk
with a TcpClient, but I don't see any examples of how to get the
TcpClient to talk with a HTTP or FTP server.
-----
Private Sub btnTestPort80_Click( ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnTestPort80.Click
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim server As String
Dim message As String
server = "localhost"
message = "GET http://localhost/default.asp HTTP/1.1"
Dim port As Int32 = 80
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte
array.
Dim data As [Byte]() =
System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)
txtResults.AppendText(Environment.NewLine & "Sent: " &
message.ToString)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
if not stream.DataAvailable then
dim x as Int32
x = 1
do
''stream = client.GetStream()
x = x + 1
if x > 100 then exit do
loop until stream.DataAvailable
end if
if stream.DataAvailable then
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0,
bytes)
end if
Console.WriteLine("Received: {0}", responseData)
txtResults.AppendText(Environment.NewLine & "Received: " &
responseData.ToString)
' Close everything.
stream.Close()
client.Close()
Catch ex As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", ex)
txtResults.AppendText(Environment.NewLine &
"ArgumentNullException: " & ex.ToString)
Catch ex As SocketException
Console.WriteLine("SocketException: {0}", ex)
txtResults.AppendText(Environment.NewLine & "SocketException: "
& ex.ToString)
Catch ex As Exception
txtResults.AppendText(Environment.NewLine & "Exception: " &
ex.ToString)
End Try
End Sub
-----