Access HTTP content over the Web

West Wind Internet Protcols supports HTTP access through the wwHTTP class. Downloading content off the Web is becoming more common all the time and wwHTTP makes the process childishly easy.

Web Content can be anything from HTML to XML,

A number of mechanisms for accessing Web content with just a few lines of code are available. Web content includes anything that is accessible via the HTTP protocol. Content can be HTML, but more importantly it can be XML, a document in some binary format or even raw data such a DBF file passed as a string. wwIPStuff supports most of the functionality that HTTP provides including GET and POST operations, secure communication over SSL/HTTPS and security via Basic Authentication.

The easiest way to grab Web content is using the HTTPGet method of wwIPStuff:

oHTTP=CREATEOBJECT("wwHTTP") lcHTML = oHTTP.HTTPGet("http://www.west-wind.com/") *** Do something with the HTML ShowHTML( lcHTML ) && in wwUtils.prg

Posting Data to a Server

HTTPGet is a very easy single method call to retrieve Web content. Although the name of the method is HttpGet you can also send data to the server via a POST operation:

oHTTP=CREATEOBJECT("wwHTTP") oHTTP.AddPostKey("FirstName","Rick") oHTTP.AddPostKey("LastName","Strahl") oHTTP.AddPostKey("Company","West Wind Technologies") *** Optionally add custom headers oHTTP.cExtraHeaders = ; "cache-control:private" + CHR(13) + CHR(10) +; "Custom-Header: Custom value" + CHR(13) + CHR(10) lcHTML = oHTTP.HTTPGet("http://www.west-wind.com/wconnect/TestPage.wwd") *** Do something with the HTML ShowHTML( lcHTML ) && in wwUtils.prg

This properly formats the HTTP POST buffer for sending the variables to the Web server which simulates an HTML form submission.

You can use the nHTTPConnectType property to set the content type used - the default is URLEncoded, which is the same as HTML forms. Other types include: 2 - Multipart forms (used often for file uploads or for forms that contain lots of data) and 4 - XML or Raw (simply posts an unencoded raw buffer).

POSTING Raw or XML Data

A common scenario is to post XML to a server for some sort of XML Service. In this case you'll need to change the POST format. For XML or Raw data set the nHttpPostMode to 4. By default this uses a Content-type of text/xml, but you can optionally override the cContentType property for other types of content.

To send XML data to a server you can use the following code:

loHttp = CREATEOBJECT('wwhttp') loHttp.nHttpPostMode = 4 && Xml or Raw *** Load up the XML data any way you need lcXML = FILETOSTR("XmlData.xml") *** Set the POST buffer - Note: No name parameter, just the buffer loHttp.AddPostKey(lcXML) lcXmlResult = loHttp.HttpGet("http://www.west-wind.com/SomeXmlService.xsvc") IF (loHttp.nError # 0) RETURN ENDIF ShowXml(lcXmlResult)

If the data you send happens to be something other than XML and the server actually cares about the content type (in most situations it doesn't) you can also set the cContentType property. For example, to send a Zip file:

loHttp.cContentType = "application/x-zip-compressed"

Streaming output directly to file

You can also stream the content of a URL directly to a file. To do this you can use additional parameters on the HttpGetMethod which takes the Url, username and password and output file as parameters.

loHttp = CREATEOBJECT('wwhttp') loHttp.HttpGet("http://www.west-wind.com/","","","c:\test.htm") IF loHttp.nError # 0 wait window loHttp.cErrorMsg return ENDIF *** Browse the file from local disk GoUrl("c:\test.htm")

Streaming to file allows you to bypass large memory usage as wwHTTP doesn't build up a large string but instead reads one buffer at a time and outputs it to disk.


  Last Updated: 3/1/2007 | © West Wind Technologies, 2008