使用UE4 HttpRequest提交多表单

本文详细介绍了如何在UnrealEngine4(UE4)中使用HttpRequest进行HTTPPOST请求,处理多表单字段,包括设置Content-Type,boundary,以及拼接字符串和文件数据的过程。

大部分HTTP库都是支持直接设置多表单字段的,但UE4的HttpRequest比较惨,只能用SetContent设置整个的TArray<uint8>作为请求体,所以想要传多表单就要自己拼。

首先设置Header,Content-Type设置为多表单,并设置boundary:

auto HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetHeader("Content-Type", TEXT("multipart/form-data; boundary=yourboundary"));
HttpRequest->SetURL(yourUrl);
HttpRequest->SetVerb("POST");

boundary想设什么都行,但要和后面用的统一。

然后拼请求体的数据字段部分:

FString RequestBody;
FString Boundary = TEXT("--yourboundary\r\nContent-Disposition: form-data; name=");
//拼入单个属性
FString Field = Boundary + TEXT("\"") + Key + TEXT("\"\r\n\r\n") + Value + TEXT("\r\n");
RequestBody += Field;

这里需要注意,分隔两个属性的boundary行要比Header里设置的boundary前面多两个横杠

然后拼入文件:

RequestBody += TEXT("--yourboundary\r\nContent-Disposition: form-data; name=\"file\"; "); //分隔行和前缀
RequestBody += TEXT("filename=\"") + FileNam
#include "HttpModule.h" #include "Interfaces/IHttpRequest.h" #include "Interfaces/IHttpResponse.h" #include "Misc/FileHelper.h" class FFileUploader { public: void UploadFile(const FString& FilePath, const FString& ServerURL) { // 读取文件到字节数组 TArray<uint8> FileData; if (!FFileHelper::LoadFileToArray(FileData, *FilePath)) { UE_LOG(LogTemp, Error, TEXT("Failed to load file: %s"), *FilePath); return; } // 创建HTTP请求 TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetVerb("POST"); HttpRequest->SetURL(ServerURL); // 设置Content-Type为multipart/form-data FString Boundary = "---------------------------" + FString::FromInt(FDateTime::Now().GetTicks()); HttpRequest->SetHeader("Content-Type", FString::Printf(TEXT("multipart/form-data; boundary=%s"), *Boundary)); // 构建multipart请求体 TArray<uint8> RequestBody; // 添加文件部分 FString FileHeader = FString::Printf( TEXT("--%s\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n"), *Boundary, *FPaths::GetCleanFilename(FilePath) ); // 添加header到请求体 RequestBody.Append((uint8*)TCHAR_TO_UTF8(*FileHeader), FileHeader.Len()); // 添加文件数据 RequestBody.Append(FileData); // 添加结束边界 FString Footer = FString::Printf(TEXT("\r\n--%s--\r\n"), *Boundary); RequestBody.Append((uint8*)TCHAR_TO_UTF8(*Footer), Footer.Len()); // 设置请求体 HttpRequest->SetContent(RequestBody); // 设置回调函数 HttpRequest->OnProcessRequestComplete().BindLambda( [this](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccess) { this->OnUploadComplete(Request, Response, bSuccess); } ); // 发送请求 HttpRequest->ProcessRequest(); } private: void OnUploadComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bSuccess) { if (bSuccess && Response.IsValid()) { int32 ResponseCode = Response->GetResponseCode(); FString ResponseContent = Response->GetContentAsString(); if (ResponseCode >= 200 && ResponseCode < 300) { UE_LOG(LogTemp, Log, TEXT("Upload successful! Response: %s"), *ResponseContent); } else { UE_LOG(LogTemp, Error, TEXT("Upload failed! Code: %d, Response: %s"), ResponseCode, *ResponseContent); } } else { UE_LOG(LogTemp, Error, TEXT("Request failed!")); } } }; ue5使用蓝图方式以及VaRest插件如何做到以上内容的请求?
最新发布
12-05
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值