Difference between revisions of "Ns httpopen"

From AOLserver Wiki
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'''
  
     % # A proc to post some data to a url and return the result
+
<pre>
     % proc postit {url data} {
+
     # A proc to post some data to a url and return the result
          set rqset [[ns_set new rqset]]
+
     proc postit {url data} {
          ns_set put $rqset "Accept" "*/*"
+
        set rqset [ns_set new rqset]
          ns_set put $rqset "User-Agent" "[[ns_info name]]-Tcl/[[ns_info version]]"
+
        ns_set put $rqset "Accept" "*/*"
        # ns_set put $rqset "Content-type" "text/xml"
+
        ns_set put $rqset "User-Agent" "[ns_info name]-Tcl/[ns_info version]"
          ns_set put $rqset "Content-type" "application/x-www-form-urlencoded"
+
        # ns_set put $rqset "Content-type" "text/xml"
          ns_set put $rqset "Content-length" [[string length $data]]
+
        ns_set put $rqset "Content-type" "application/x-www-form-urlencoded"
 
+
        ns_set put $rqset "Content-length" [string length $data]
          set timeout 15
+
        set timeout 15
 
+
        set connInfo [ns_httpopen POST $url $rqset $timeout $data]
          set connInfo [[ns_httpopen POST $url $rqset $timeout $data]]
+
        foreach {rfd wfd headers} $connInfo break
 
+
        close $wfd
          foreach {rfd wfd headers} $connInfo {break}
+
        set length [ns_set iget $headers content-length]
          close $wfd
+
        if {[string match "" $length]} {
 
+
            set length -1
          set length [[ns_set iget $headers content-length]]
+
        }
          if {[[string match "" $length]]} {
+
        set page ""
              set length -1
+
        set err [catch {
          }
+
            # Read the content.
 
+
            while {1} {
          set page ""
+
                set buf [_ns_http_read $timeout $rfd $length]
          set err [[catch {
+
                append page $buf
              # Read the content.
+
                if {[string match "" $buf]} {
              while {1} {
+
                    break
                  set buf [_ns_http_read $timeout $rfd $length]]
+
                }
                  append page $buf
+
                if {$length > 0} {
                  if {[[string match "" $buf]]} {
+
                    incr length -[string length $buf]
                      break
+
                    if {$length <= 0} {
                  }
+
                        break
                  if {$length > 0} {
+
                    }
                      incr length -[[string length $buf]]
+
                }
                      if {$length <= 0} {
+
            }
                          break
+
        } errMsg]
                      }
+
        ns_set free $headers
                  }
+
        close $rfd
              }
+
        if {$err} {
          } errMsg]
+
            return -code error -errorinfo $::errorInfo $errMsg
 
+
        }
          ns_set free $headers
+
        return $page
          close $rfd
+
    }
          if {$err} {
+
</pre>
              global errorInfo
 
              return -code error -errorinfo $errorInfo $errMsg
 
          }
 
          return $page
 
      }
 
  
 
'''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

ns_http, ns_httpget, ns_httppost