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

相关推荐
大數據精準工單獲取7 小时前
【数据抓取】 编写爬虫基本请求:使用爬虫框架发送 HTTP 请求,获取网页内容
爬虫·网络协议·http
时空自由民.8 小时前
HTTP协议和MQTT协议区别
网络·网络协议·http
AI_Claude_code9 小时前
网络基础回顾:DNS、IP封锁与HTTP/S协议关键点
网络·爬虫·python·tcp/ip·http·爬山算法·安全架构
robch1 天前
python3 -m http.server 8001直接启动web服务类似 nginx
前端·nginx·http
以太浮标1 天前
华为eNSP模拟器综合实验之- DHCP、DNS、HTTP和FTP服务器配置案例Client-Server
linux·服务器·windows·http·华为·信息与通信
杨凯凡1 天前
【002】HTTPS 粗解:证书、TLS 握手与对后端配置的影响
网络协议·http·https
AI_Claude_code1 天前
ZLibrary访问困境方案二:DNS-over-HTTPS/TLS配置与隐私保护实践
爬虫·python·网络协议·http·网络安全·https·网络爬虫
张np1 天前
java框架和http调用接口的区别
java·开发语言·http
江畔何人初1 天前
http协议的概念以及http1,http2,http3的区别
网络·网络协议·http
AI_Claude_code1 天前
ZLibrary访问困境方案六:自建RSS/Calibre内容同步服务器的完整指南
运维·服务器·网络·爬虫·python·tcp/ip·http