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

一、新建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文件,重新生成这些文件。

相关推荐
win x10 小时前
深入理解HTTPS协议加密流程
网络协议·http·https
仙俊红10 小时前
从 Filter / Interceptor 到 HTTPS
网络协议·http·https
liann11911 小时前
3.1_网络——基础
网络·安全·web安全·http·网络安全
三水不滴1 天前
计算机网络核心网络模型
经验分享·笔记·tcp/ip·计算机网络·http·https
SunflowerCoder1 天前
基于插件化 + Scriban 模板引擎的高效 HTTP 协议中心设计
http·c#
Remember_9931 天前
MySQL 索引详解:从原理到实战优化
java·数据库·mysql·spring·http·adb·面试
Zach_yuan2 天前
从零理解 HTTP:协议原理、URL 结构与简易服务器实现
linux·服务器·网络协议·http
JQLvopkk2 天前
C# 实现Http Json格式 Post 、Get 方法请求 winform服务器
http·c#·json
csdn2015_3 天前
Spring Boot `HttpServletRequest`
spring boot·http·servlet
Re.不晚3 天前
JAVA进阶之路——网络通信的层级密码:Socket切入,理解TCP与HTTP协议
java·tcp/ip·http