HttpClient (gb.net.curl)
这个类提供了一个HTTP客户端,该客户端向HTTP服务器发送请求并接收响应。
Inherited static properties
静态方法
属性
Inherited properties
方法
Inherited methods
Inherited events
Example
' How to download a file from the internet synchronously
' Put the "Async" property set to FALSE prior to Get(), that way
' Get() will stop the program flow until all the information is
' received. In that case you should use also the "TimeOut" property to
' set a timeout. If not, it could hang forever if the server does not
' reply properly.
Public Sub GetFile()
Dim hClient As HttpClient
Dim sBuffer As String
hClient = New HttpClient AS "hClient"
hClient.URL = "http://elinks.or.cz/"
hClient.Async = FALSE
hClient.Timeout = 60
hClient.Get
Print "Begin"
If hClient.Status < 0 Then
Print "ERROR"
Else
' Success - read the data
If Lof(hClient) Then sBuffer = Read #hClient, Lof(hClient)
Print sBuffer
End If
Print "end"
End
Example #2
This second example shows how you can download a file from the internet asynchronously.
It calls the
DownloadAsync
method with your
URL. Then, when the download is completed, it displays the received HTML in the Finished event handler.
' How to download a file from the internet asynchronously
Public hAsyncClient As New HttpClient AS "hAsyncClient"
Private sDownloadBuffer As String
Public Sub DownloadAsync(URL As String)
sDownloadBuffer = ""
hAsyncClient.URL = URL
hAsyncClient.TimeOut = 20
hAsyncClient.Async = TRUE
hAsyncClient.Get()
End
Public Sub hAsyncClient_Connect()
Print "Connected to " & hAsyncClient.URL
End
Public Sub hAsyncClient_Read()
Dim sBuffer As String
sBuffer = Read #Last, Lof(Last)
sDownloadBuffer &= sBuffer
End
Public Sub hAsyncClient_Error()
Print "Error " & hAsyncClient.Status & " while downloading " & hAsyncClient.URL
End
Public Sub hAsyncClient_Finished()
Print sDownloadBuffer
End