UE5 C++自定义Http节点获得Header数据

一、新建C++文件

UE5 C++自定义Http节点获得Header数据_第1张图片 

选择All Classes,选择父类BlueprintFunctionLibrary,命名为SendHttpRequest。UE5 C++自定义Http节点获得Header数据_第2张图片

UE5 C++自定义Http节点获得Header数据_第3张图片

添加Http支持

UE5 C++自定义Http节点获得Header数据_第4张图片

代理回调的参数使用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&,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 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

UE5 C++自定义Http节点获得Header数据_第5张图片

使用UE_LOG做测试还挺好用。

问题:no value will be returned by reference

数组作为代理的参数的写法

DECLARE_DYNAMIC_DELEGATE_TwoParams(FHttpResponseDelegate,bool,bSuccess,const TArray&,headers);

问题:[Compiler] In use pin    no longer exists on node  Send Http Request . 

删除项目如下文件夹,然后鼠标右键点击uproject文件,重新生成这些文件。

UE5 C++自定义Http节点获得Header数据_第6张图片UE5 C++自定义Http节点获得Header数据_第7张图片

你可能感兴趣的:(Unreal,Engine,1024程序员节,unreal,http,blueprint)