一、新建C++文件
选择All Classes,选择父类BlueprintFunctionLibrary,命名为SendHttpRequest。
添加Http支持
代理回调的参数使用DECLARE_DYNAMIC_DELEGATE_TwoParam定义,第一参数是代理类型,后面是参数1类型,参数1,参数2类型,参数2。
代理通过UPROPERTY声明
UFUNCTION的BlueprintCallable是定义一个带有流程的节点
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "SendHttpRequest.generated.h"
DECLARE_DYNAMIC_DELEGATE_TwoParams(FHttpResponseDelegate,bool,bSuccess,const TArray<FString>&,headers);
UCLASS()
class REDSTORY_API USendHttpRequest : public UBlueprintFunctionLibrary
{
GENERATED_BODY() public:
UPROPERTY()
FHttpResponseDelegate HttpResponseDelegate;
UFUNCTION(BlueprintCallable, Category = "Custom", meta = (Keywords = "SendHttpRequest"))
static void SendHttpRequest(FHttpResponseDelegate Callback);
// void Response(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "SendHttpRequest.h"
#include "HttpModule.h"
#include "Interfaces/IHttpResponse.h"
void USendHttpRequest::SendHttpRequest(FHttpResponseDelegate Callback)
{
FHttpModule* Http = &FHttpModule::Get();
TSharedRef<IHttpRequest,ESPMode::ThreadSafe> Request = Http->CreateRequest();
Request->SetURL("http://127.0.0.1:8888/a");
Request->SetVerb("POST");
Request->SetHeader("Content-Type", "application/json");
Request->SetHeader("User-Agent","X-UnrealEngine-Agent");
Request->SetContentAsString("{\"a\": 1,\"b\": \"2\",\"c\": \"3\",\"d\": \"4\"}");
// Request->OnProcessRequestComplete().BindUObject(this,&USendHttpRequest::Response);
Request->OnProcessRequestComplete().BindLambda([Callback](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
//UE_LOG(LogTemp,Display,TEXT("Response %s"),*Response->GetHeader("Set-Cookie"));
Callback.ExecuteIfBound(bWasSuccessful,Response->GetAllHeaders());
});
Request->ProcessRequest();
}
// void USendHttpRequest::Response(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded)
// {
//
// //UE_LOG(LogTemp,Display,TEXT("Response %s"),*HttpResponse->GetContentAsString());
// // UE_LOG(LogTemp,Display,TEXT("Response %s"),*HttpResponse->GetHeader("Set-Cookie"));
// // HttpResponseDelegate.Broadcast(bSucceeded);
// }
二、Blueprint
使用UE_LOG做测试还挺好用。
问题:no value will be returned by reference
数组作为代理的参数的写法
DECLARE_DYNAMIC_DELEGATE_TwoParams(FHttpResponseDelegate,bool,bSuccess,const TArray<FString>&,headers);
问题:[Compiler] In use pin <Unnamed> no longer exists on node Send Http Request .
删除项目如下文件夹,然后鼠标右键点击uproject文件,重新生成这些文件。