├── Resources └── Icon128.png ├── Images ├── GetHttpRequest.jpg ├── PostHttpRequest.jpg ├── GetHttpRequestWithBaseUrl.jpg └── PostHttpRequestWithBaseUrl.jpg ├── Source └── InHttpClient │ ├── Public │ ├── InHttpClient.h │ └── InHttpClientBPLibrary.h │ ├── Private │ ├── InHttpClient.cpp │ └── InHttpClientBPLibrary.cpp │ └── InHttpClient.Build.cs ├── InHttpClient.uplugin ├── README.md ├── LICENSE └── .gitignore /Resources/Icon128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inveta/InHttpClient/HEAD/Resources/Icon128.png -------------------------------------------------------------------------------- /Images/GetHttpRequest.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inveta/InHttpClient/HEAD/Images/GetHttpRequest.jpg -------------------------------------------------------------------------------- /Images/PostHttpRequest.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inveta/InHttpClient/HEAD/Images/PostHttpRequest.jpg -------------------------------------------------------------------------------- /Images/GetHttpRequestWithBaseUrl.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inveta/InHttpClient/HEAD/Images/GetHttpRequestWithBaseUrl.jpg -------------------------------------------------------------------------------- /Images/PostHttpRequestWithBaseUrl.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inveta/InHttpClient/HEAD/Images/PostHttpRequestWithBaseUrl.jpg -------------------------------------------------------------------------------- /Source/InHttpClient/Public/InHttpClient.h: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | #pragma once 4 | 5 | #include "Modules/ModuleManager.h" 6 | 7 | class FInHttpClientModule : public IModuleInterface 8 | { 9 | public: 10 | 11 | /** IModuleInterface implementation */ 12 | virtual void StartupModule() override; 13 | virtual void ShutdownModule() override; 14 | }; 15 | -------------------------------------------------------------------------------- /InHttpClient.uplugin: -------------------------------------------------------------------------------- 1 | { 2 | "FileVersion": 3, 3 | "Version": 1, 4 | "VersionName": "1.0", 5 | "FriendlyName": "InHttpClient", 6 | "Description": "", 7 | "Category": "Other", 8 | "CreatedBy": "", 9 | "CreatedByURL": "", 10 | "DocsURL": "", 11 | "MarketplaceURL": "", 12 | "SupportURL": "", 13 | "CanContainContent": true, 14 | "IsBetaVersion": false, 15 | "IsExperimentalVersion": false, 16 | "Installed": false, 17 | "Modules": [ 18 | { 19 | "Name": "InHttpClient", 20 | "Type": "Runtime", 21 | "LoadingPhase": "PreLoadingScreen" 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # InHttpClient 2 | 基于UE C++的http库封装的蓝图http库,只需要一个函数即可完成http的调用 3 | 4 | # 使用方法 5 | ## baseurl相同使用方法 6 | 针对前后端分离架构,通过设置baseurl来设置服务器的ip地址,然后后续调用只需要设置subroute即可。 7 | 可以通过AddHeader来添加token验证这种http头信息。 8 | 注意:这个库只支持设置一个baseurl。 9 | GetHttpRequest方法和PostHttpRequest方法可以多次调用。使用示例如下: 10 | ![GetHttpRequest](./Images/GetHttpRequest.jpg) 11 | ![PostHttpRequest](./Images/PostHttpRequest.jpg) 12 | 13 | 14 | ## 多个不同baseurl使用方法 15 | 另外考虑到能够兼容不同baseurl地址的http请求,我们还设计了带有baseurl参数的http请求,同样一个函数搞定了所有。 16 | GetHttpRequestWithBaseUrl方法和PostHttpRequestWithBaseUrl方法可以多次调用。 17 | ![GetHttpRequestWithBaseUrl](./Images/GetHttpRequestWithBaseUrl.jpg) 18 | ![PostHttpRequestWithBaseUrl](./Images/PostHttpRequestWithBaseUrl.jpg) 19 | -------------------------------------------------------------------------------- /Source/InHttpClient/Private/InHttpClient.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Epic Games, Inc. All Rights Reserved. 2 | 3 | #include "InHttpClient.h" 4 | 5 | #define LOCTEXT_NAMESPACE "FInHttpClientModule" 6 | 7 | void FInHttpClientModule::StartupModule() 8 | { 9 | // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module 10 | 11 | } 12 | 13 | void FInHttpClientModule::ShutdownModule() 14 | { 15 | // This function may be called during shutdown to clean up your module. For modules that support dynamic reloading, 16 | // we call this function before unloading the module. 17 | 18 | } 19 | 20 | #undef LOCTEXT_NAMESPACE 21 | 22 | IMPLEMENT_MODULE(FInHttpClientModule, InHttpClient) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 inveta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Visual Studio 2015 user specific files 2 | .vs/ 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | 22 | # Compiled Static libraries 23 | *.lai 24 | *.la 25 | *.a 26 | *.lib 27 | 28 | # Executables 29 | *.exe 30 | *.out 31 | *.app 32 | *.ipa 33 | 34 | # These project files can be generated by the engine 35 | *.xcodeproj 36 | *.xcworkspace 37 | *.sln 38 | *.suo 39 | *.opensdf 40 | *.sdf 41 | *.VC.db 42 | *.VC.opendb 43 | 44 | # Precompiled Assets 45 | SourceArt/**/*.png 46 | SourceArt/**/*.tga 47 | 48 | # Binary Files 49 | Binaries/* 50 | Plugins/*/Binaries/* 51 | 52 | # Builds 53 | Build/* 54 | 55 | # Whitelist PakBlacklist-.txt files 56 | !Build/*/ 57 | Build/*/** 58 | !Build/*/PakBlacklist*.txt 59 | 60 | # Don't ignore icon files in Build 61 | !Build/**/*.ico 62 | 63 | # Built data for maps 64 | *_BuiltData.uasset 65 | 66 | # Configuration files generated by the Editor 67 | Saved/* 68 | 69 | # Compiled source files for the engine to use 70 | Intermediate/* 71 | Plugins/*/Intermediate/* 72 | 73 | # Cache files for the editor to use 74 | DerivedDataCache/* 75 | -------------------------------------------------------------------------------- /Source/InHttpClient/InHttpClient.Build.cs: -------------------------------------------------------------------------------- 1 | // Some copyright should be here... 2 | 3 | using UnrealBuildTool; 4 | 5 | public class InHttpClient : ModuleRules 6 | { 7 | public InHttpClient(ReadOnlyTargetRules Target) : base(Target) 8 | { 9 | PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; 10 | 11 | PublicIncludePaths.AddRange( 12 | new string[] { 13 | // ... add public include paths required here ... 14 | } 15 | ); 16 | 17 | 18 | PrivateIncludePaths.AddRange( 19 | new string[] { 20 | // ... add other private include paths required here ... 21 | } 22 | ); 23 | 24 | 25 | PublicDependencyModuleNames.AddRange( 26 | new string[] 27 | { 28 | "Core", 29 | // ... add other public dependencies that you statically link with here ... 30 | } 31 | ); 32 | 33 | 34 | PrivateDependencyModuleNames.AddRange( 35 | new string[] 36 | { 37 | "CoreUObject", 38 | "Engine", 39 | "Slate", 40 | "SlateCore", 41 | // ... add private dependencies that you statically link with here ... 42 | "HTTP", 43 | } 44 | ); 45 | 46 | 47 | DynamicallyLoadedModuleNames.AddRange( 48 | new string[] 49 | { 50 | // ... add any modules that your module loads dynamically here ... 51 | } 52 | ); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Source/InHttpClient/Public/InHttpClientBPLibrary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Kismet/BlueprintAsyncActionBase.h" 4 | #include "Runtime/Online/HTTP/Public/Http.h" 5 | #include "InHttpClientBPLibrary.generated.h" 6 | 7 | UCLASS(BlueprintType, Blueprintable) 8 | class UInHttpClientBPLibrary : public UBlueprintAsyncActionBase 9 | { 10 | GENERATED_BODY() 11 | public: 12 | 13 | DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FInHttpEvent, const FString&, MessageString); 14 | UPROPERTY(EditAnywhere, BlueprintReadOnly, BlueprintAssignable, Category = InHttpClient) 15 | FInHttpEvent OnSuccessEvent; 16 | 17 | UPROPERTY(EditAnywhere, BlueprintReadOnly, BlueprintAssignable, Category = InHttpClient) 18 | FInHttpEvent OnFailEvent; 19 | 20 | virtual void Activate(); 21 | 22 | public: 23 | UFUNCTION(BlueprintCallable, Category = "InHttpClient") 24 | static void AddHeader(const FString& HeaderName, const FString& HeaderValue); 25 | 26 | UFUNCTION(BlueprintCallable, Category = "InHttpClient") 27 | static void SetBaseUrl(const FString& BaseUrl); 28 | 29 | UFUNCTION(BlueprintCallable, Category = "InHttpClient") 30 | static UInHttpClientBPLibrary* PostHttpRequest(const FString& SubRoute, const FString& Content); 31 | 32 | UFUNCTION(BlueprintCallable, Category = "InHttpClient") 33 | static UInHttpClientBPLibrary* GetHttpRequest(const FString& SubRoute); 34 | 35 | UFUNCTION(BlueprintCallable, Category = "InHttpClient") 36 | static UInHttpClientBPLibrary* PostHttpRequestWithBaseUrl(const FString& BaseUrl, const FString& SubRoute, const FString& Content, const TMap& Headers); 37 | 38 | UFUNCTION(BlueprintCallable, Category = "InHttpClient") 39 | static UInHttpClientBPLibrary* GetHttpRequestWithBaseUrl(const FString& BaseUrl, const FString& SubRoute, const TMap& Headers); 40 | 41 | public: 42 | static TMap m_RequestHeaders; 43 | static FString m_BaseUrl; 44 | static TArray m_ListInHttp; 45 | public: 46 | FString m_SubRoute; 47 | FString m_Content; 48 | FString m_Verb; 49 | FString m_LocalBaseUrl; 50 | TMap m_LocalRequestHeaders; 51 | bool m_UseBaseUrl = true; 52 | private: 53 | void HttpResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful); 54 | }; 55 | 56 | -------------------------------------------------------------------------------- /Source/InHttpClient/Private/InHttpClientBPLibrary.cpp: -------------------------------------------------------------------------------- 1 | #include "InHttpClientBPLibrary.h" 2 | #include "InHttpClient.h" 3 | 4 | 5 | TMap UInHttpClientBPLibrary::m_RequestHeaders{ 6 | {"Accept","*/*"} 7 | }; 8 | 9 | FString UInHttpClientBPLibrary::m_BaseUrl; 10 | TArray UInHttpClientBPLibrary::m_ListInHttp; 11 | 12 | void UInHttpClientBPLibrary::Activate() 13 | { 14 | TSharedRef Request = FHttpModule::Get().CreateRequest(); 15 | 16 | FString httpUrl = UInHttpClientBPLibrary::m_BaseUrl + m_SubRoute; 17 | if (false == m_UseBaseUrl) 18 | { 19 | httpUrl = m_LocalBaseUrl + m_SubRoute; 20 | } 21 | Request->SetURL(httpUrl); 22 | 23 | if (true == m_UseBaseUrl) 24 | { 25 | for (auto& item : UInHttpClientBPLibrary::m_RequestHeaders) 26 | { 27 | Request->SetHeader(item.Key, item.Value); 28 | } 29 | } 30 | else 31 | { 32 | for (auto& item : m_LocalRequestHeaders) 33 | { 34 | Request->SetHeader(item.Key, item.Value); 35 | } 36 | } 37 | 38 | Request->SetVerb(m_Verb); 39 | Request->SetContentAsString(m_Content); 40 | 41 | Request->OnProcessRequestComplete().BindUObject(this, &UInHttpClientBPLibrary::HttpResponse); 42 | auto isSuccess = Request->ProcessRequest(); 43 | if (false == isSuccess) 44 | { 45 | UInHttpClientBPLibrary::m_ListInHttp.Remove(this); 46 | OnFailEvent.Broadcast(""); 47 | } 48 | } 49 | 50 | void UInHttpClientBPLibrary::AddHeader(const FString& HeaderName, const FString& HeaderValue) 51 | { 52 | m_RequestHeaders.Add(HeaderName, HeaderValue); 53 | } 54 | void UInHttpClientBPLibrary::SetBaseUrl(const FString& BaseUrl) 55 | { 56 | m_BaseUrl = BaseUrl; 57 | } 58 | UInHttpClientBPLibrary* UInHttpClientBPLibrary::PostHttpRequest(const FString& SubRoute, const FString& Content) 59 | { 60 | UInHttpClientBPLibrary* ptr = NewObject(); 61 | ptr->m_SubRoute = SubRoute; 62 | ptr->m_Content = Content; 63 | ptr->m_Verb = "POST"; 64 | 65 | m_ListInHttp.Add(ptr); 66 | return ptr; 67 | } 68 | UInHttpClientBPLibrary* UInHttpClientBPLibrary::GetHttpRequest(const FString& SubRoute) 69 | { 70 | UInHttpClientBPLibrary* ptr = NewObject(); 71 | ptr->m_SubRoute = SubRoute; 72 | ptr->m_Verb = "GET"; 73 | 74 | m_ListInHttp.Add(ptr); 75 | return ptr; 76 | } 77 | UInHttpClientBPLibrary* UInHttpClientBPLibrary::PostHttpRequestWithBaseUrl(const FString& BaseUrl, const FString& SubRoute, const FString& Content,const TMap& Headers) 78 | { 79 | UInHttpClientBPLibrary* ptr = NewObject(); 80 | ptr->m_SubRoute = SubRoute; 81 | ptr->m_Content = Content; 82 | ptr->m_Verb = "POST"; 83 | ptr->m_LocalBaseUrl = BaseUrl; 84 | ptr->m_LocalRequestHeaders = Headers; 85 | 86 | ptr->m_UseBaseUrl = false; 87 | m_ListInHttp.Add(ptr); 88 | return ptr; 89 | } 90 | 91 | UInHttpClientBPLibrary* UInHttpClientBPLibrary::GetHttpRequestWithBaseUrl(const FString& BaseUrl, const FString& SubRoute, const TMap& Headers) 92 | { 93 | UInHttpClientBPLibrary* ptr = NewObject(); 94 | ptr->m_SubRoute = SubRoute; 95 | ptr->m_Verb = "GET"; 96 | ptr->m_LocalBaseUrl = BaseUrl; 97 | ptr->m_LocalRequestHeaders = Headers; 98 | ptr->m_UseBaseUrl = false; 99 | 100 | m_ListInHttp.Add(ptr); 101 | return ptr; 102 | } 103 | 104 | void UInHttpClientBPLibrary::HttpResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) 105 | { 106 | UInHttpClientBPLibrary::m_ListInHttp.Remove(this); 107 | 108 | if (false == bWasSuccessful) 109 | { 110 | OnFailEvent.Broadcast(""); 111 | return; 112 | } 113 | if (false == Response.IsValid()) 114 | { 115 | OnFailEvent.Broadcast(""); 116 | return; 117 | } 118 | if (false == EHttpResponseCodes::IsOk(Response->GetResponseCode())) 119 | { 120 | OnFailEvent.Broadcast(""); 121 | return; 122 | } 123 | FString JsonString = Response->GetContentAsString(); 124 | OnSuccessEvent.Broadcast(JsonString); 125 | } --------------------------------------------------------------------------------