Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed - 30 Aug 2007 15:19 GMT
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

-----
Vadym Stetsiak - 30 Aug 2007 17:34 GMT
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> -----
Ed - 30 Aug 2007 18:27 GMT
> Hello, Ed!
>
[quoted text clipped - 9 lines]
>
> 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.
Vadym Stetsiak - 31 Aug 2007 08:28 GMT
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> On Aug 30, 12:34 pm, "Vadym Stetsiak" <vadm...@gmail.com> wrote:
>> Hello, Ed!

>> Try to config HttpWebRequest like this

>> <configuration>
>> <system.net>
[quoted text clipped - 3 lines]
>> </system.net>
>> </configuration>

>> HTH

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.
Enrico Zerilli - 10 Sep 2007 15:06 GMT
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
Signature

Enrico Zerilli
enricozerilli@hotmail.com
http://blogs.dotmark.net/enf/

> > Hello, Ed!
> >
[quoted text clipped - 19 lines]
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.