Question Http Get with images using Tcpclient

choru

New member
Joined
Jun 11, 2008
Messages
2
Programming Experience
1-3
Okay so I'm a little bit confused as to why I can't fix my problem -the thing is I have a tcpclient and I want to make http requests using it - I type the IP address of the target server and it's port which is 80 it all works fine when I'm requesting a page it's just I can't figure out how to get the full response for any image which I try to get, for example if I make a request for Google's favicon using the following:

GET /favicon.ico HTTP/1.1
Host: www.google.co.uk
the response which I'm given by the server is just:

HTTP/1.1 200 OK
Content-Type: image/x-icon
Last-Modified: Fri, 30 May 2008 06:03:19 GMT
Expires: Sun, 17 Jan 2038 19:14:07 GMT
Date: Wed, 11 Jun 2008 12:46:08 GMT
Server: gws
Content-Length: 1150

It tells me the content length but not the actual content - How do I get the content???

Below is my code.

Code:
 Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect(TextBox1.Text, TextBox2.Text)
        Dim networkStream As Net.Sockets.NetworkStream = tcpClient.GetStream()
        If networkStream.CanWrite And networkStream.CanRead Then
            Dim sendBytes As [Byte]() = Encoding.UTF8.GetBytes(RichTextBox2.Text)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            Dim returndata As String = Encoding.UTF8.GetString(bytes)
            MsgBox(returndata)
        End If
I hope someone can help me - thanks.
 
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,336
Location
Norway
Programming Experience
10+
It do tell you the content, according to RFC the headers are followed by a double linefeed then content bytes, possibly separated by multipart separators specified. Here is a basic sample extending yours:
Code:
Dim reader As New IO.StreamReader(tcpClient.GetStream, System.Text.Encoding.ASCII)
Dim contentLength As Integer
Do
    Dim line As String = reader.ReadLine
    If line.StartsWith("Content-Length:") Then
        contentLength = Integer.Parse(line.Substring(16))
    ElseIf line = "" Then
        Dim content(contentLength - 1) As Byte
        networkStream.Read(content, 0, content.Length)
        My.Computer.FileSystem.WriteAllBytes("content.ico", content, False)
        Exit Do
    End If
Loop
I'm guessing you have a special interest here, and not just want the file:
Code:
My.Computer.Network.DownloadFile("http://www.google.co.uk/favicon.ico", "googicon.ico")
 

choru

New member
Joined
Jun 11, 2008
Messages
2
Programming Experience
1-3
Thanks for your help - but unfortunately I keep getting the error

"Object reference not set to an instance of an object."

With the "If line.starts with" line.

I've tried messing with it, changing it and moving it around but it's just the same - it's probably something glaringly obvious though that I'm missing right??
 
Top Bottom