How to get a web server to send response using TcpClient & NetworkStream?

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

-----
 
V

Vadym Stetsiak

Hello, Ed!

Try to config HttpWebRequest like this

<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>

HTH

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Thu, 30 Aug 2007 07:19:22 -0700:

E> Background: I'm attempting to access a web page hosted on a server
E> embedded in firmware on a device. I initially tried using the
E> HttpWebRequest (which I know is coded properly since it works with
E> other web servers) but the server embedded in the firmware is
E> returning an error and the reponse object is empty. This is curious
E> to me as the same page returns without errors when viewed in a
E> browser.

E> The error is:
E> System.Net.WebException was caught
E> Message="The server committed a protocol violation.
E> Section=ResponseStatusLine"
E> Source="System"
E> StackTrace:
E> at System.Net.HttpWebRequest.GetResponse()
E> at WWIP.frmMain.PollRouter()
E> ServerProtocolViolation {11}

E> I would like to attempt to perform the same task at a lower level to
E> see exactly what it being returned from the server in order to
E> diagnose the problem and work around it. I'm attempting to use the
E> TcpClient class and a slightly modified version of the sample code in
E> the MSDN documentation for this class:

E> http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcpclient(
E> vs.80).aspx

E> What do I need to "write" to the NetworkStream to initiate a response
E> from the server? I'm guessing some format of an HTTP Request, but I'm
E> not sure the format of if that's even correct? Currently, I'm testing
E> with my local IIS and the NetworkStream.DataAvailable property is
E> always false, so it appears that nothing is being received from the
E> server on port 80.

E> There are pleny of examples of how to code my own TcpListener to talk
E> with a TcpClient, but I don't see any examples of how to get the
E> TcpClient to talk with a HTTP or FTP server.

E> -----
 
E

Ed

Hello, Ed!

Try to config HttpWebRequest like this

<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>

HTH

That worked great!
I created an application configuration file and set the
useUnsafeHeaderParsing option to true and now the HttpWebRequest
returns data!

Thanks!

If you or anyone does know the answer to my TcpClient NetworkStream
port question, I'd still like to know how to do that too.
 
V

Vadym Stetsiak

Hello, Ed!

In your "TcpClient NetworkStream port" you for incorrect HTTP requests.
Consult with HTTP protocol RFC for details.
(http://www.w3.org/Protocols/rfc2616/rfc2616.html)

For example, HTTP request line must end with double CRLF.

stiring message = "GET http://localhost/default.asp HTTP/1.1\r\n\r\n";

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com



You wrote on Thu, 30 Aug 2007 10:27:07 -0700:



E> That worked great!
E> I created an application configuration file and set the
E> useUnsafeHeaderParsing option to true and now the HttpWebRequest
E> returns data!

E> Thanks!

E> If you or anyone does know the answer to my TcpClient NetworkStream
E> port question, I'd still like to know how to do that too.
 
G

Guest

Hi
i have the same problem but the solution don't working!!!
I have put the syntax
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>
in the web.config file under the path C:\Windows\Microsoft.NET\V 2.0 ...
\CONFIG
but the problem exist too!!.
How can i do for solve this bad situation?

My Clinet is't a BizTalk 2006 channel that send a message by a HTTP Send
Port .

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top