home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13h.zip / dopost.cmd < prev    next >
OS/2 REXX Batch file  |  1999-04-07  |  3KB  |  116 lines

  1. /* Call as:  DOPUT serveraddress url_to_use filename                   */
  2. /* ------------------------------------------------------------------- */
  3. /* This program requires that the RxSock.DLL be in your LIBPATH (it is */
  4. /* usually in your \TCPIP\DLL directory.  It was shipped with the      */
  5. /* August 1994 CSD for the TCP/IP base (UN64092).                      */
  6.  
  7. call load /* load functions if necessary */
  8.  
  9. say " Program to test the POST verb "
  10.  
  11. say " Issue a POST method request to an HTTP server, and display complete response "
  12. parse arg server request .
  13. if server="" then do 
  14.     mehost=get_hostname()
  15.     say " Please enter server address (ENTER= " mehost ": "
  16.     parse pull server
  17.     if server="" then server=mehost
  18. end  /* Do */
  19. parse var server server ':' httpport
  20. if httpport='' then httpport=80
  21. if request="" then  do
  22.   say " Please enter url to POST: "
  23.   parse pull request
  24. end
  25.  
  26. use_Url=request
  27.  
  28. say " Enter a message string to include in body  (ENTER=use a file name)"
  29. parse pull amessage
  30. if amessage="" then do
  31.    say " enter file name to include in body "
  32.    parse pull afile
  33.    amessage=charin(afile,1,chars(afile))
  34.    aa=stream(afile,'c','close')
  35. end /* do */
  36.  
  37. amessage=translate(amessage,'+',' ')
  38.  
  39. crlf    ='0d0a'x                        /* constants */
  40. family  ='AF_INET'
  41.  
  42. rc=sockgethostbyname(server, "serv.0")  /* get dotaddress of server */
  43. if rc=0 then do; say 'Unable to resolve "'server'"'; exit; end
  44. dotserver=serv.0addr                    /* .. */
  45.  
  46. gosaddr.0family=family                  /* set up address */
  47. gosaddr.0port  =httpport
  48. gosaddr.0addr  =dotserver
  49.  
  50. gosock = SockSocket(family, "SOCK_STREAM", "IPPROTO_TCP")
  51.  
  52.  
  53. message='POST ' use_url ' HTTP/1.1'crlf
  54. message=message||'Content-type:application/x-www-form-urlencoded'||crlf
  55. message=message||'Content-length:'||length(amessage)||crlf
  56. message=message||'Connection: close'crlf
  57. message=message||'Host: 'server||crlf
  58.  
  59. message=message||crlf||amessage
  60.  
  61. got=''
  62. rc = SockConnect(gosock,"gosaddr.0")
  63. if rc<0 then do; say 'Unable to connect to "'server'"'; exit; end
  64. rc = SockSend(gosock, message)
  65. say " bytes transfered = " rc " .. waiting for reply .... "
  66.  
  67. /* Now wait for the response */
  68. do r=1 by 1
  69.   rc = SockRecv(gosock, "response", 1000)
  70.   got=got||response
  71.    say '>'rc'/>' ||length(response)
  72.   if rc<=0 then leave
  73.   end r
  74. rc = SockClose(gosock)
  75.  
  76. say 'Got' length(got) 'bytes of response'
  77. say " Hit ENTER to see response headers ... "
  78. parse pull ooo
  79. dd=crlf||crlf
  80. parse var got  hd (dd) bd
  81. say "Reponse Headers:"
  82. say hd
  83. say ; say " ENTER to see response body,or a filename ... "
  84. parse pull ooo
  85. if ooo="" then
  86.   say  bd
  87. else
  88.   wow=charout(ooo,bd,1)
  89. exit
  90.  
  91. /* --- Load the function library, if necessary --- */
  92. load:
  93. if \RxFuncQuery("SockLoadFuncs") then return      /* already there */
  94. call RxFuncAdd "SockLoadFuncs","rxSock","SockLoadFuncs"
  95. call SockLoadFuncs
  96. return
  97.  
  98.  
  99.  
  100. /* get the hostname (aa.bb.cc) for this machine
  101.    Developed by Timur Kazimirov  */
  102.  
  103. get_hostname:procedure
  104. if \RxFuncQuery("SockLoadFuncs")
  105.   then
  106.     nop
  107.   else
  108.     do
  109.       call RxFuncAdd "SockLoadFuncs","rxSock","SockLoadFuncs"
  110.       call SockLoadFuncs
  111.     end
  112. dot_addr = SockGetHostId()
  113. rc = SockGetHostByAddr(dot_addr, "host.")
  114. return host.name
  115.  
  116.