Difference between revisions of "Ns httpopen"
Jump to navigation
Jump to search
(improve proc -- old form did not seem to work for me.) |
m (clean up formatting of example code) |
||
Line 23: | Line 23: | ||
'''EXAMPLES''' | '''EXAMPLES''' | ||
− | + | <pre> | |
− | + | # A proc to post some data to a url and return the result | |
− | + | proc postit {url data} { | |
− | + | set rqset [ns_set new rqset] | |
− | + | ns_set put $rqset "Accept" "*/*" | |
− | + | ns_set put $rqset "User-Agent" "[ns_info name]-Tcl/[ns_info version]" | |
− | + | # ns_set put $rqset "Content-type" "text/xml" | |
− | + | ns_set put $rqset "Content-type" "application/x-www-form-urlencoded" | |
− | + | ns_set put $rqset "Content-length" [string length $data] | |
− | + | set timeout 15 | |
− | + | set connInfo [ns_httpopen POST $url $rqset $timeout $data] | |
− | + | foreach {rfd wfd headers} $connInfo break | |
− | + | close $wfd | |
− | + | set length [ns_set iget $headers content-length] | |
− | + | if {[string match "" $length]} { | |
− | + | set length -1 | |
− | + | } | |
− | + | set page "" | |
− | + | set err [catch { | |
− | + | # Read the content. | |
− | + | while {1} { | |
− | + | set buf [_ns_http_read $timeout $rfd $length] | |
− | + | append page $buf | |
− | + | if {[string match "" $buf]} { | |
− | + | break | |
− | + | } | |
− | + | if {$length > 0} { | |
− | + | incr length -[string length $buf] | |
− | + | if {$length <= 0} { | |
− | + | break | |
− | + | } | |
− | + | } | |
− | + | } | |
− | + | } errMsg] | |
− | + | ns_set free $headers | |
− | + | close $rfd | |
− | + | if {$err} { | |
− | + | return -code error -errorinfo $::errorInfo $errMsg | |
− | + | } | |
− | + | return $page | |
− | + | } | |
− | + | </pre> | |
− | |||
− | |||
− | |||
− | |||
− | |||
'''SEE ALSO''' | '''SEE ALSO''' |
Latest revision as of 18:55, 22 November 2005
<manpage>ns_httpopen</manpage>
NAME
- ns_httpopen - Fetch a web page
SYNOPSIS
- ns_httpopen method url ?rqset? ?timeout? ?pdata?
DESCRIPTION
- This command opens an http connection to a web page used to fetch its contents via the method, which may be POST, GET, or another valid http method. If the act of opening the connections fails, it may throw an error.
- The url may be either absolute (http://server/page) or local (/page). If it is local, the host and port number of the server are added.
- The parameter rqset is the handle for an ns_set containing headers to send with the request. The timeout is the number of seconds to wait for the connection to open. Pdata is the extra data to send with the request (i.e. post data). If pdata is specified, the caller is responsible for supplying the Content-length and Content-type headers in rqset
- The call returns a list with these three elements: a file descriptor for reading, a file descriptor for writing, and a set ID for a set describing the connection.
- Note: This command is currently implemented in Tcl, in the source file tcl/http.tcl
EXAMPLES
# A proc to post some data to a url and return the result proc postit {url data} { set rqset [ns_set new rqset] ns_set put $rqset "Accept" "*/*" ns_set put $rqset "User-Agent" "[ns_info name]-Tcl/[ns_info version]" # ns_set put $rqset "Content-type" "text/xml" ns_set put $rqset "Content-type" "application/x-www-form-urlencoded" ns_set put $rqset "Content-length" [string length $data] set timeout 15 set connInfo [ns_httpopen POST $url $rqset $timeout $data] foreach {rfd wfd headers} $connInfo break close $wfd set length [ns_set iget $headers content-length] if {[string match "" $length]} { set length -1 } set page "" set err [catch { # Read the content. while {1} { set buf [_ns_http_read $timeout $rfd $length] append page $buf if {[string match "" $buf]} { break } if {$length > 0} { incr length -[string length $buf] if {$length <= 0} { break } } } } errMsg] ns_set free $headers close $rfd if {$err} { return -code error -errorinfo $::errorInfo $errMsg } return $page }
SEE ALSO