Difference between revisions of "Ns httpopen"

From AOLserver Wiki
Jump to navigation Jump to search
m (clean up formatting of example code)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Man page: http://aolserver.com/docs/tcl/ns_httpopen.html
+
<manpage>ns_httpopen</manpage>
 
 
See also: [[Ns_http]]
 
 
 
----
 
  
 
'''NAME'''
 
'''NAME'''
Line 27: 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-length" [[string length $data]]
+
        # ns_set put $rqset "Content-type" "text/xml"
 
+
        ns_set put $rqset "Content-type" "application/x-www-form-urlencoded"
          set timeout 15
+
        ns_set put $rqset "Content-length" [string length $data]
 
+
        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
          foreach {rfd wfd headers} $connInfo {break}
+
        close $wfd
          close $wfd
+
        set length [ns_set iget $headers content-length]
 
+
        if {[string match "" $length]} {
          set length [[ns_set iget $headers content-length]]
+
            set length -1
          if {[[string match "" $length]]} {
+
        }
              set length -1
+
        set page ""
          }
+
        set err [catch {
 
+
            # Read the content.
          set page ""
+
            while {1} {
          set err [[catch {
+
                set buf [_ns_http_read $timeout $rfd $length]
              # Read the content.
+
                append page $buf
              while {1} {
+
                if {[string match "" $buf]} {
                  set buf [_ns_http_read $timeout $rfd $length]]
+
                    break
                  append page $buf
+
                }
                  if {[[string match "" $buf]]} {
+
                if {$length > 0} {
                      break
+
                    incr length -[string length $buf]
                  }
+
                    if {$length <= 0} {
                  if {$length > 0} {
+
                        break
                      incr length -[[string length $buf]]
+
                    }
                      if {$length <= 0} {
+
                }
                          break
+
            }
                      }
+
        } errMsg]
                  }
+
        ns_set free $headers
              }
+
        close $rfd
          } errMsg]
+
        if {$err} {
 
+
            return -code error -errorinfo $::errorInfo $errMsg
          ns_set free $headers
+
        }
          close $rfd
+
        return $page
          if {$err} {
+
    }
              global errorInfo
+
</pre>
              return -code error -errorinfo $errorInfo $errMsg
 
          }
 
          return $page
 
      }
 
  
 
'''SEE ALSO'''
 
'''SEE ALSO'''
Line 78: Line 70:
 
: [[ns_http]], [[ns_httpget]], [[ns_httppost]]
 
: [[ns_http]], [[ns_httpget]], [[ns_httppost]]
  
----
+
[[Category:Core Tcl API]]
 
 
[[Category Documentation]] - [[Category Core Tcl API]]
 

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