| Home Page | Recent Changes | Preferences

LibHTTP/Example

First make sure you set up the environment correctly. Make sure LibHTTP4 is in the EditPackages list and above your package.

This example will explain the most basic usage of LibHTTP. The example class will simply request a document from the internet and save it to a file.

00001  class MyLibHTTPExample extends Info;
00002  
00003  event PostBeginPlay()
00004  {
00005    local HttpSock socket;
00006    socket = spawn(class'HttpSock');
00007    socket.OnComplete = MyOnComplete;
00008    socket.ClearRequestData();
00009    socket.get("http://wiki.beyondunreal.com/wiki/LibHTTP");
00010  }
00011  
00012  function MyOnComplete(HttpSock Sender)
00013  {
00014    local FileLog flog;
00015    local int i;
00016    flog = spawn(class'FileLog');
00017    flog.OpenLog("MyLibHTTPExample", "html", true);
00018    for (i = 0; i < Sender.ReturnData.length; i++)
00019      flog.Logf(Sender.ReturnData[i]);
00020    flog.Destroy();
00021  }

On line #6 we create the main class. The /HttpSock class is the main class of LibHTTP and in most cases the only class you will need to address. On line #7 we set the OnComplete delegate. This delegate is called when the HTTP request was completed. This doesn't mean the HTTP request was succesful (e.g. returned a HTTP response code 200), it just means that LibHTTP was able to perform a successful request and retrieve data from the request location. Next we call the function ClearRequestData() (line #8). Although there is actually no need to call this function in this example, it is quite important in the future. ClearRequestData() will clean up all the data from the previous request. So in case of subsequent requests using the same HttpSock class you should call this function before doing a request, otherwise you might send data to the server you don't want (for example a username and password).

The last function called in the PostBeginPlay() is get(...). Calling this function will start the HTTP request (using a GET request, which is the most common). The get(...) function accepts the complete URL to the document you want to retrieve. The URL must be properly encoded or else the request might fail. The URL is used AS IS, so if it's not properly encoded the webserver might not respond correctly. If you don't know if the URL is properly encoded you can also enter that URL is your webbrowser, most webbrowsers will automatically convert the URL to be properly encoded.

After the get(...) is might take a while before the request is done. However, a lot of things can go wrong. If something goes wrong in the preparation of the request the get(...) will return false (also the OnError delegate is called). It could also happen that the webserver isn't reachable (either a connection timeout or the hostname won't resolve), check the /HttpSock for more information about this. In this example we assume everything works out.

When the HTTP request is complete, and the whole document has been downloaded, the OnComplete delegate is called. In this case it will call our MyOnComplete function (line #12). The HttpSock class contains a lot of variables you can access, most important are ReturnData and LastStatus. The LastStatus variable will contain the HTTP response code send by the server. This should have the value 200, if it doesn't have the value 200 it means that there was something wrong with the requested data (for example a value of 404 means the file wasn't found, and 500 means the webserver is broken in some way). The ReturnData variable contains the complete body of the requested document, this is usually what you are interested in. In our example we simply save all the data in the ReturnData to a log file. Every entry in the ReturnData array is a single line without the newline characters.

LibHTTP contains a lot of features and functionality, be sure to check out the documentation of /HttpSock to check how other things can be done.

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

FAQs

Help Desk

Mapping Topics

Mapping Lessons

UnrealEd Interface

UnrealScript Topics

UnrealScript Lessons

Making Mods

Class Tree

Modeling Topics

Chongqing Page

Log In