When you're maintaining scoreboards or other such things that require regular HTTP request access to servers, you can use the HTTP API to perform such web request tasks.
Have a server to which you're allowed to request data via HTTP. You can use a public server of any type to try out HTTP requests if you'd like.
ProjectName.Build.cs
file.HttpModule.h
header file, the HttpManager.h
header file, and the HttpRetrySystem.h
file, as shown in the following code snippet:#include "Runtime/Online/HTTP/Public/HttpManager.h" #include "Runtime/Online/HTTP/Public/HttpModule.h" #include "Runtime/Online/HTTP/Public/HttpRetrySystem.h"
IHttpRequest
object from FHttpModule
using the following code:TSharedRef<IHttpRequest> http=FHttpModule::Get().CreateRequest();
IHttpRequest
object's FHttpRequestCompleteDelegate
, which has a signature as follows:void HttpRequestComplete( FHttpRequestPtr request, FHttpResponsePtr response, bool success );
IHttpRequest
object as http->OnProcessRequestComplete()
:FHttpRequestCompleteDelegate& delegate = http->OnProcessRequestComplete();
There are a few ways to attach a callback function to the delegate. You can use the following:
delegate.BindLambda()
:delegate.BindLambda( // Anonymous, inlined code function (aka lambda) []( FHttpRequestPtr request, FHttpResponsePtr response, bool success ) -> void { UE_LOG( LogTemp, Warning, TEXT( "Http Response: %d, %s" ), request->GetResponse()->GetResponseCode(), *request->GetResponse()->GetContentAsString() ); });
delegate.BindUObject( this, &AChapter12GameMode::HttpRequestComplete );
.BindRaw
:PlainObject* plainObject = new PlainObject(); delegate.BindRaw( plainObject, &PlainObject::httpHandler ); // plainObject cannot be DELETED Until httpHandler gets called..
You have to ensure that your plainObject
refers to a valid object in memory at the time the HTTP request completes. This means that you cannot use TAutoPtr
on plainObject
, because that will deallocate plainObject
at the end of the block in which it is declared, but that may be before the HTTP request completes.
// C-style function for handling the HTTP response: void httpHandler( FHttpRequestPtr request, FHttpResponsePtr response, bool success ) { Info( "static: Http req handled" ); } delegate.BindStatic( &httpHandler );
When using a delegate callback with an object, be sure that the object instance that you're calling back on lives on at least until the point at which the HttpResponse
arrives back from the server. Processing the HttpRequest
takes real time to run. It is a web request after all—think of waiting for a web page to load.
You have to be sure that the object instance on which you're calling the callback function has not deallocated on you between the time of the initial call and the invocation of your HttpHandler
function. The object must still be in memory when the callback returns after the HTTP request completes.
You cannot simply expect that the HttpResponse
function happens immediately after you attach the callback function and call ProcessRequest()
! Using a reference counted UObject
instance to attach the HttpHandler
member function is a good idea to ensure that the object stays in memory until the HTTP request completes.
http->SetURL( TEXT( "http://unrealengine.com" ) );
ProcessRequest
:http->ProcessRequest();
The HTTP object is all you need to send off HTTP requests to a server and get HTTP responses. You can use the HTTP request/response for anything that you wish; for example, submitting scores to a high scores table or to retrieve text to display in-game from a server.
They are decked out with a URL to visit and a function callback to run when the request is complete. Finally, they are sent off via FManager
. When the web server responds, your callback is called and the results of your HTTP response can be shown.
18.220.163.91