Solved

Download something through http(Winsock)

Posted on 2000-04-13
5
370 Views
Last Modified: 2013-11-13
How can i download a webpage with a winsock control. i just really need the rules for asking for files. i'm familiar with winsock but not http.
0
Comment
Question by:hess
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
ATM earned 25 total points
ID: 2714090
- To feel it:
Open telnet console
set LocalEcho-on, BlinkingCursor-on, VT-100/ANSI, all others off.
connect to www.tair.freeservers.com port 80 using connect menu.
type
GET / HTTP/1.0
press enter twise.
- To write simple code.
1. add winsock to form - winsock1,form1
2. create Fetch - button1
3. add textbox/multiline/VScroll - text1
4. winsock1_onclick

text1.text=""
winsock1.connect "www.tair.freeservers.com",80

5. winsock1_onconnect

Dim outData as String
outData="GET / HTTP/1.0" & vbcrlf & vbcrlf
winsock1.send outData

6. winsock1_ondataarrival
Dim inData as String
wisock1.getdata inData,vbstring
text1.text=text1.text & indata

7. winsock1_onerror
  winsock1.close

8. winsock1_close
  winsock1.close

- To more info:
www.tair.freeservers.com

GoodLuck.
0
 
LVL 8

Expert Comment

by:stefanx
ID: 2714111
Damn, someone beat me to it. Here is my answer as a comment :

The HTTP Protocol is very simple. To view precisely how it works, the best would be to write a socket listener program that listens on port 80, and then make your browser connect to it (if it is the same machine, just navigate to http://127.0.0.1/getme.htm). There are 2 common HTTP protocols (HTTP1.0 and HTTP1.1). HTTP 1.0 allows you to simply specify the file that your wish to retrieve (for example GET index.html) while HTTP1.1 requires that each GET request be prepended by the full site path (i.e GET http://myserver.com/index.html). The latter is very popular because it allows you to run multiple websites off one IP Address (with HTTP1.0, each website hosted on your server must be on a separate IP Address since the server does not receive header information on which of the sites you actually want to retrieve data from.

When posting infomation (for example in response to an HTML <FORM METHOD=POST ACTION=....> directive, the transport mechanism is still HTTP, but contains a different HTTP command and the form data.

Here are 2 examples, one with a normal HTTP request (no form data, also know as an HTTP GET command), and an HTTP request with form data attached (HTTP POST command).

In both these examples, you will find reference to a Timer control called TimerWinsock. This is simpy a timeout mechanism which works by setting the Timer Control's interval to something reasonable (say 10 seconds). The onTimer event simply disables the timer i.e

'-----------------------
Sub WinsockTimer_Timer()
  WinsockTimer.Enabled = False
End Sub
'------------------------

The routines also require a public variable called WinsockRec$ (preferably defined in a module) that is used by the Winsock_DataArival event as follows:

'----------------------------------
Private Sub Winsock_DataArrival(Index AS Integer, ByVal bytesTotal AS Long)

  Dim Q$
  Winsock(Index).GetData Q$,vbString
  WinsockRec$ = WinsockRec$ & Q$
End Sub
'-----------------------------------

Get Command:

Here is a routine for getting data from a webserver :

'-------------------------------------
Private Function GetResult(ByVal Host$, ByVal Doc As String, ByVal TimeOutValue AS Long, ByRef Result As String) As Boolean

  ' This function uses the embedded Winsock control to
  ' request a page Doc from Host$ using HTTP 1.1
  ' The function returns a boolean to indicate success or failure (true = success)
  ' with either the data or an error message returned in the Byref passes String Result
 
  On Local Error Resume Next
 
  Dim Z$
 
  Load Winsock(1)
  Err = 0
  Winsock(1).RemoteHost = Host$
  Winsock(1).RemotePort = 80
  TimerWinsock.Interval = TimeoutValue
  TimerWinsock.Enabled = True
  Winsock(1).Connect
  While TimerWinsock.Enabled And (Winsock(1).State <> sckConnected)
    DoEvents
  Wend
  TimerWinsock.Enabled = False
  If Winsock(1).State = sckConnected Then
    Z$ = "GET " & "http://" & Host$ & Doc & " HTTP/1.1" & vbCrLf & vbcrlf
    TimerWinsock.Interval = TimeoutValue
    WinsockRec$ = ""
    TimerWinsock.Enabled = True
    Winsock(1).SendData Z$
    While (Winsock(1).State <> sckClosing) And TimerWinsock.Enabled
      DoEvents
    Wend
    TimerWinsock.Enabled = False
    If Winsock(1).State = sckClosing Then
      Winsock(1).Close
      Result = WinsockRec$
      Unload Winsock(1)
      GetResult = True
      Exit Function
    End If
    Winsock(1).Close
    Unload Winsock(1)
    Result = "Data not received in time"
    GetResult = False
  Else
    Winsock(1).Close
    Unload Winsock(1)
       Result = "Failed to connect to Server"
    GetResult = False
  End If

End Function
'--------------------------------

Post Command:

Here is code to show how how to send data using a post command and retrieving the response from the Server:


'---------------------------------
Private Function PostResult(ByVal Host$, ByVal Q$, ByVal Doc As String, ByRef Result As String) As Boolean

  ' This function uses the embedded Winsock control to
  ' send POST type data to a document. The Data to post is passed
  ' in Q$, the document to post to in Doc and the result in Result.
  ' The function returns a boolean to indicate success or failure.
 
  On Local Error Resume Next
 
  Dim Z$
 
'  If Q$ = "" Then
'    PostResult = False
'    Exit Function
'  End If
  If Doc = "" Then
    PostResult = False
    Exit Function
  End If
  Load Winsock(1)
  Err = 0
  Winsock(1).RemoteHost = Host$
  Winsock(1).RemotePort = 80
  TimerWinsock.Interval = TimeoutValue
  TimerWinsock.Enabled = True
  Winsock(1).Connect
  While TimerWinsock.Enabled And (Winsock(1).State <> sckConnected)
    DoEvents
  Wend
  TimerWinsock.Enabled = False
  If Winsock(1).State = sckConnected Then
    Z$ = "POST " & "http://" & Host$ & Doc & " HTTP/1.1" & vbCrLf
    Z$ = Z$ & "Content-Type: application/x-www-form-urlencoded" & vbCrLf
    Z$ = Z$ & "Content-Length: " & Format$(Len(Q$)) & vbCrLf
    Z$ = Z$ & "Connection: Keep-Alive" & vbCrLf & vbCrLf
    Z$ = Z$ & Q$ & vbCrLf
    TimerWinsock.Interval = TimeoutValue
    WinsockRec$ = ""
    TimerWinsock.Enabled = True
    Winsock(1).SendData Z$
    While (Winsock(1).State <> sckClosing) And TimerWinsock.Enabled
      DoEvents
    Wend
    TimerWinsock.Enabled = False
    If Winsock(1).State = sckClosing Then
      Winsock(1).Close
      Result = WinsockRec$
      Unload Winsock(1)
      PostResult = True
      Exit Function
    End If
    Winsock(1).Close
    Unload Winsock(1)
    PostResult = False
  Else
    Winsock(1).Close
    Unload Winsock(1)
    PostResult = False
  End If

End Function

'-----------------------------

The performance of the Winsock Control is not the greatest for retrieving data from the server. In some cases (especially if your program and the server are on the same LAN, the winsock control receives the Close event before all the data has been retrieved. This is especially true with IIS4 (IIS3 doesn't do this). I found that the Inet control is slightly better than the Winsock control for this, so I generally use it for the GET portion. Unfortunately its POST just simply does not work. A workaround for the "close before all data received" problem is not to wait for the Winsock(1).State = sckclosed, but to wait for a terminator string (normally </HTML>). However, this assumes that website coders have properly coded their HTML which very often is not the case.

Hope that helps you


0
 
LVL 1

Expert Comment

by:ATM
ID: 2714141
I not agree Your Inet component love. Winsock always more fast than first one. All errors can be avoid using proper coding.
0
 
LVL 8

Expert Comment

by:stefanx
ID: 2714166
ATM - Are you suggesting that my coding is "improper"? Keep in mind that the Inet control is probably written entirely in C or C++ and hence it should inherently be faster in processing Winsock calls that the Winsock control can have its events processed by VB. The only way to work with the Winsock stack at optimum speed and efficiency is to use the API directly anyway.

Judging by your language use, English is not your native language, so maybe you did not mean to insult me, but if you have a problem with a fellow experts's code, quantify this with more than a lame one liner, OK ?
0
 
LVL 1

Expert Comment

by:ATM
ID: 2714381
:) no, I didn't meant what your code "improper", of course ...  
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now