Click here to Skip to main content
15,896,557 members

Downloading a webpage using socket in VB.NET

Joeadeoye asked:

Open original thread
Hello friends, how can i download a webpage using vb.net socket. I know how to do this using TcpClient() but i want to do it using socket. I want to do something like this:
VB
Dim cs As New TcpClient
cs.Connect("www.example.com", 80)
Dim nw as NetworkStream = cs.GetStream()
Dim buffer(1024) As Byte
Dim hdr_request As String = "GET / HTTP/1.1" & VbNewLine
hdr_request = hdr_request & "Host: www.example.com"  & VbNewLine
hdr_request = hdr_request & "Connection: Close"  & VbNewLine & VbNewLine

Dim toSend As Byte() = Encoding.ASCII.GetBytes(hdr_request)
nw.Write(toSend, 0, toSend.Length)
nw.flush()

'reading response
Dim c As Integer
While True
   c = nw.Read(buffer, 0, buffer.Length)
   If c = 0 Then
      cs.Close()
      Exit While
   End If
   Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, c))
End While
Console.Writeline("DONE")


I want to do something like this using socket. The above example is done using TcpClient but i want to re-do it using socket, send(), recv() e.t.c.
Please help me re-write the above example using socket instead of TcpClient i used!
Please Help Me!!!!!

My second question is that can a TcpClient connect to SSL socket?
I try to connect something like tcpClient.Connect("www.google.com" 443) i got an error "No Such Host is Known" i even try tcpClient.Connect("ssl://www.google.com", 443) i got the same error. But if i do it in PHP using the fsockopen() function to connect i.e. fsockopen("ssl://www.google.com", 443) it works well. So what is wrong with vb.net
Tags: Visual Basic, Sockets

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900