File: http_debug_example.tcl

package info (click to toggle)
tcltls 1.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: ansic: 4,349; tcl: 1,392; sh: 408; makefile: 32
file content (37 lines) | stat: -rw-r--r-- 918 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#################################################
#
# Download webpage using HTTP package with debug output
#
#################################################

package require Tcl 8.6-
package require tls
package require http

set url "https://www.tcl.tk/"
set port 443
set protocol "http/1.1"

# Register https protocol handler with http package
http::register https 443 [list ::tls::socket -autoservername 1 -require 1 -alpn [list [string tolower $protocol]] \
    -command ::tls::callback -password ::tls::password -validatecommand ::tls::validate_command]

# Get webpage
set token [::http::geturl $url -blocksize 16384]
if {[http::status $token] ne "ok"} {
    puts [format "Error: \"%s\"" [http::status $token]]
    ::http::cleanup $token
    exit
}

# Get web page
set data [http::data $token]

# Cleanup
::http::cleanup $token

# Save data to file
set ch [open "tcl_tk_home.html" wb]
puts $ch $data
close $ch