当前位置:   article > 正文

【UE4】HTTP下载_unreal engine http模块文件下载

unreal engine http模块文件下载

纯接口调用,直接上代码
Build.cs中添加HTTP模块

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore"  , "HTTP"});
  • 1

.h

	void DownLoad(FString URL);
	//下载完成调用
	void HandleDownloadRequest(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded);
	//处理下载进度
	void HandleHttpRequestProgress(FHttpRequestPtr HttpRequest, int32 BytesSent, int32 BytesReceived);
  • 1
  • 2
  • 3
  • 4
  • 5

.cpp

void UTestInstance::DownLoad(FString URL) {
	TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
	HttpRequest->OnProcessRequestComplete().BindUObject(this, &UTestInstance::HandleDownloadRequest);
	HttpRequest->OnRequestProgress().BindUObject(this, &UTestInstance::HandleHttpRequestProgress);
	HttpRequest->SetURL(URL);
	HttpRequest->SetVerb(TEXT("GET"));
	HttpRequest->ProcessRequest();
}
void UTestInstance::HandleDownloadRequest(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded) {
	if (!HttpRequest.IsValid() || !HttpResponse.IsValid()) {
		return;
	}
	else if (bSucceeded && EHttpResponseCodes::IsOk(HttpResponse->GetResponseCode())) {
		//保存文件
		FString Filename = FPaths::GetCleanFilename(HttpRequest->GetURL());
		FString Left;
		FString Right;
		Filename.Split("?", &Left, &Right);
		FFileHelper::SaveArrayToFile(HttpResponse->GetContent(), *FString::Printf(TEXT("%s%s"), *FPaths::ProjectContentDir(), *Left));
	}
}

void UTestInstance::HandleHttpRequestProgress(FHttpRequestPtr HttpRequest, int32 BytesSent, int32 BytesReceived) {
	//Your code
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/91216
推荐阅读
相关标签
  

闽ICP备14008679号