// Fill out your copyright notice in the Description page of Project Settings. #include "HttpReqActor.h" // Sets default values UHttpReqActor::UHttpReqActor() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. } // Called when the game starts or when spawned void UHttpReqActor::BeginPlay() { Super::BeginPlay(); } /*Http call*/ bool UHttpReqActor::Post(FString URL,FString ContentString) { //TSharedRef Request = Http->CreateRequest(); //Request->OnProcessRequestComplete().BindUObject(this, &UHttpReqActor::OnResponseReceived); ////This is the url on which to process the request //Request->SetURL(URL); //Request->SetVerb("POST"); //Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent"); //Request->SetContentAsString(ContentString); //Request->SetHeader("Content-Type", "text/javascript;charset=utf-8"); //Request->ProcessRequest(); //UE_LOG(LogTemp, Log, TEXT("aaaaaaaaaaaaaa======%s")); // 创建Http 请求 TSharedRef Request = FHttpModule::Get().CreateRequest(); // 设置请求头 Request->SetHeader("Content-Type", "text/javascript;charset=utf-8"); // 设置请求方式 Request->SetVerb("POST"); // 请求的链接 Request->SetURL(URL); // 服务端预留的测试接口 // 内容包 Request->SetContentAsString(ContentString); // 设置回调函数 Request->OnProcessRequestComplete().BindUObject(this, &UHttpReqActor::OnResponseReceived); // 发送请求 Request->ProcessRequest(); return 0; } /*Assigned function on successfull http call*/ void UHttpReqActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) { //GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, Response->GetContentAsString()); //UE_LOG(LogTemp, Log, TEXT("FHttpResponsePtr======%s"), *Response->GetContentAsString()); ////Create a pointer to hold the json serialized data //TSharedPtr JsonObject; ////Create a reader pointer to read the json data //TSharedRef> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString()); ////Deserialize the json data given Reader and the actual object to deserialize //if (FJsonSerializer::Deserialize(Reader, JsonObject)) //{ // TSharedPtr recievedObject = JsonObject->GetObjectField("Exam"); // //Get the value of the json object by field name // FString recievedStr = recievedObject->GetStringField("ScoreStep"); // //Output it to the engine // GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, recievedStr); if (OnPostResponseReceived.IsBound())//检查是否存在有效绑定 { OnPostResponseReceived.Broadcast(Response->GetContentAsString()); } //} }