HttpReqActor.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "HttpReqActor.h"
  3. // Sets default values
  4. UHttpReqActor::UHttpReqActor()
  5. {
  6. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  7. }
  8. // Called when the game starts or when spawned
  9. void UHttpReqActor::BeginPlay()
  10. {
  11. Super::BeginPlay();
  12. }
  13. /*Http call*/
  14. bool UHttpReqActor::Post(FString URL,FString ContentString)
  15. {
  16. //TSharedRef<IHttpRequest> Request = Http->CreateRequest();
  17. //Request->OnProcessRequestComplete().BindUObject(this, &UHttpReqActor::OnResponseReceived);
  18. ////This is the url on which to process the request
  19. //Request->SetURL(URL);
  20. //Request->SetVerb("POST");
  21. //Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
  22. //Request->SetContentAsString(ContentString);
  23. //Request->SetHeader("Content-Type", "text/javascript;charset=utf-8");
  24. //Request->ProcessRequest();
  25. //UE_LOG(LogTemp, Log, TEXT("aaaaaaaaaaaaaa======%s"));
  26. // 创建Http 请求
  27. TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
  28. // 设置请求头
  29. Request->SetHeader("Content-Type", "text/javascript;charset=utf-8");
  30. // 设置请求方式
  31. Request->SetVerb("POST");
  32. // 请求的链接
  33. Request->SetURL(URL); // 服务端预留的测试接口
  34. // 内容包
  35. Request->SetContentAsString(ContentString);
  36. // 设置回调函数
  37. Request->OnProcessRequestComplete().BindUObject(this, &UHttpReqActor::OnResponseReceived);
  38. // 发送请求
  39. Request->ProcessRequest();
  40. return 0;
  41. }
  42. /*Assigned function on successfull http call*/
  43. void UHttpReqActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
  44. {
  45. //GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, Response->GetContentAsString());
  46. //UE_LOG(LogTemp, Log, TEXT("FHttpResponsePtr======%s"), *Response->GetContentAsString());
  47. ////Create a pointer to hold the json serialized data
  48. //TSharedPtr<FJsonObject> JsonObject;
  49. ////Create a reader pointer to read the json data
  50. //TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
  51. ////Deserialize the json data given Reader and the actual object to deserialize
  52. //if (FJsonSerializer::Deserialize(Reader, JsonObject))
  53. //{
  54. // TSharedPtr<FJsonObject> recievedObject = JsonObject->GetObjectField("Exam");
  55. // //Get the value of the json object by field name
  56. // FString recievedStr = recievedObject->GetStringField("ScoreStep");
  57. // //Output it to the engine
  58. // GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, recievedStr);
  59. if (OnPostResponseReceived.IsBound())//检查是否存在有效绑定
  60. {
  61. OnPostResponseReceived.Broadcast(Response->GetContentAsString());
  62. }
  63. //}
  64. }