Mostly Harmless


From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

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.

  1. class MyLibHTTPExample extends Info;
  3. event PostBeginPlay()
  4. {
  5.   local HttpSock socket;
  6.   socket = spawn(class'HttpSock');
  7.   socket.OnComplete = MyOnComplete;
  8.   socket.ClearRequestData();
  9.   // The URL has to be complete for example: you must specify the http:// part.
  10.   socket.get("");
  11. }
  13. function MyOnComplete(HttpSock Sender)
  14. {
  15.   local FileLog flog;
  16.   local int i;
  17.   flog = spawn(class'FileLog');
  18.   flog.OpenLog("MyLibHTTPExample", "html", true);
  19.   // Log each retrieved line.
  20.   for (i = 0; i < Sender.ReturnData.length; i++)
  21.     flog.Logf(Sender.ReturnData[i]);
  22.   flog.Destroy();
  23. }

On line #6 we create the main class. The Legacy:LibHTTP/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 successful (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(...) it 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 Legacy:LibHTTP/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 Legacy:LibHTTP/HttpSock to check how other things can be done.