Download

HTTP Response from a PHP File

Hello guys, I am really new to C++ because I am a C#/Java Programmer and I asked myself if it is possible to get a http response after transmitting variables like c# does.

For example, here is my php code which is working fine:

<?php
$host="localhost"; // Der Hostname
$username="user"; // Euer MySQL Username
$password="testpass"; // Euer MySQL Passwort
$db_name="game"; // Der Datenbankname
$tbl_name="user"; // Der Name des Tables
if (!isset($_POST'usernametosend']) or !isset($_POST'passwordtosend'])) die("The username or password is missing!");
// Hier verbinden wir uns mit dem Datenbankserver und selektieren die Datenbank
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("Cannot select Database");
$usernametosend=$_POST'usernametosend'];
$passwordtosend=$_POST'passwordtosend'];
// Dies ist ein Schutz gegen allgemeine SQL Injektionen
if (@get_magic_quotes_gpc())
{
    $usernametosend = stripslashes($usernametosend);
    $passwordtosend = stripslashes($passwordtosend);
}
$usernametosend = @mysql_real_escape_string($usernametosend);
$passwordtosend = @mysql_real_escape_string($passwordtosend);
$sql="SELECT * FROM $tbl_name WHERE username='$usernametosend' and password='$passwordtosend'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
@mysql_free_result($result);
if($count==1)
{
    // Hier wird unsere Session registriert und accepted
    $_SESSION'usernametosend'] = $usernametosend;
    $_SESSION'passwordtosend'] = $passwordtosend;
    echo("accepted");
}
else echo("Wrong username or password"); //Der Login ist fehlgeschlagen
@mysql_close();
?>

So if everything is fine I will get a “accepted” message and if something is wrong “Wrong username or password”.

In C# it works fine with this:



var request = (HttpWebRequest)WebRequest.Create("http://testdomain.net/login.php");

            var postData = "usernametosend="+textBox1.Text;
            postData += "&passwordtosend=" + textBox2.Text;
            var data = Encoding.ASCII.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            
            MessageBox.Show(responseString);


quiet easy or ?
But how can I do something like this in UE4 ? I wrote all the response posts and didnt got an idea … pls help…

Ty guys

hello 3HMonkey please check this link https://answers.unrealengine.com/questions/165223/fhttpmodule-usage-issues.html i found very useful, UE class is even more useful then c#, even i made a blueprint static node you can do the same
Greetings…

hey and thank you for helping…

So i started building this in relation to https://answers.unrealengine.com/questions/165223/fhttpmodule-usage-issues.html
First I started with the build.cs:



// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class LoginTest : ModuleRules
{
	public LoginTest(TargetInfo Target)
	{
		PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore", "HTTP" });
        PrivateDependencyModuleNames.AddRange(new string] { "HTTP" });
        PrivateIncludePathModuleNames.AddRange(new string] { "HTTP" });
	}
}



I created a UBlueprintFunctionLibrary like this .h:



#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "Runtime/Online/HTTP/Public/Http.h"
#include "Http.h" 
#include "IHttpRequest.h"
#include "HttpWrapper.generated.h"

/**
 * 
 */

UCLASS()
class LOGINTEST_API UHttpWrapper : public UBlueprintFunctionLibrary
{
	GENERATED_UCLASS_BODY()
			

	UFUNCTION(BlueprintCallable, Category = "HttpWrapper")
	 void completedHTTPRequest(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful);
	

	UFUNCTION(BlueprintCallable, Category = "HttpWrapper")
		void createRequest();
	
	
};


And here the .cpp:



// Fill out your copyright notice in the Description page of Project Settings.

#include "LoginTest.h"
#include "HttpWrapper.h"
#include "IHttpRequest.h"




void UHttpWrapper::createRequest()
{
	TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
	Request->SetVerb(TEXT("POST"));
	Request->SetURL("http://www....");
	// setcontent-type to whatever you want
	Request->SetHeader("Content-Type", "application/json");
	Request->SetContentAsString("...");
	
		// bind a function which gets called when the request is completed
	Request->OnProcessRequestComplete().BindUObject(this, &UHttpWrapper::completedHTTPRequest);
	
	if (!Request->ProcessRequest())
	{
		// HTTP request failed
	}
}

void UHttpWrapper::completedHTTPRequest(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful)
{
	if (!response.IsValid())
	{
		// no valid response
	}
	else if (EHttpResponseCodes::IsOk(response->GetResponseCode()))
	{
		// valid response
		FString msg = response->GetContentAsString();
	}
	else
	{
		// HTTP request error
	}
}


But Visual Studio is coming up with this error and I cannot find a solution :frowning:


error : In HttpWrapper: Unrecognized type 'FHttpRequestPtr'