NaviServer - programmable web server

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

# ns_httpopen(n) 4.99.8 naviserver "NaviServer Built-in Commands"

## Name

ns_httpopen - Fetch a web page

## Table Of Contents

## Synopsis

- ns_httpopen method url ?rqset? ?timeout? ?pdata?

## Description

This command is deprecated; use ns_http instead.

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 () 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

## COMMANDS

ns_httpopen method url ?rqset? ?timeout? ?pdata?

## 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

ns_http, ns_httpget, ns_httppost, ns_httptime

## Keywords

http-client