- Home /
The question is answered, right answer was accepted
POST to HTTP not working
I've been trying to get this tutorial to work with UnityWebRequest. GET works fine, but when I try to POST I keep getting this error:
register.php:
Error: Cannot connect to destination host
UnityEngine.Debug:Log(Object)
<Register>d__10:MoveNext() (at Assets/Scripts/RegistrationMenu.cs:63)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Both register.php
and test.php
work as expected in browser. Here's the C# code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.Networking;
public class RegistrationMenu : MonoBehaviour
{
[SerializeField] GameObject MainPanel;
[SerializeField] GameObject ScorePanel;
[SerializeField] TMP_InputField NameField;
[SerializeField] TMP_InputField PasswordField;
[SerializeField] TMP_InputField PasswordConfirmField;
[SerializeField] Button SubmitButton;
[SerializeField] string RegisterURL = "http://184.88.11.231/gardenlife/register.php";
public void Start()
{
StartCoroutine(test());
}
private IEnumerator test()
{
var TestURL = "http://184.88.11.231/gardenlife/test.php";
UnityWebRequest webRequest = UnityWebRequest.Get(TestURL);
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
string[] pages = TestURL.Split('/');
int page = pages.Length - 1;
if (webRequest.isNetworkError)
{
Debug.Log(pages[page] + ": Error: " + webRequest.error);
}
else
{
Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
}
}
public void CallRegister()
{
StartCoroutine(Register());
}
private IEnumerator Register()
{
WWWForm form = new WWWForm();
form.AddField("name", NameField.text);
form.AddField("password", PasswordField.text);
UnityWebRequest webRequest = UnityWebRequest.Post(RegisterURL, form);
webRequest.chunkedTransfer = false;
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
string[] pages = RegisterURL.Split('/');
int page = pages.Length - 1;
if (webRequest.isNetworkError)
{
Debug.Log(pages[page] + ":\nError: " + webRequest.error);
}
else
{
Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
}
}
public void GoToMain()
{
MainPanel.SetActive(true);
this.gameObject.SetActive(false);
}
public void GoToScore()
{
ScorePanel.SetActive(true);
this.gameObject.SetActive(false);
}
public void VerifyInputs()
{
SubmitButton.interactable = (NameField.text.Length >= 8 && PasswordField.text.Length >= 8 && PasswordField.text == PasswordConfirmField.text);
}
}
Any advice would be greatly appreciated.
Answer by MatchaJoeJoe · Jul 30, 2020 at 05:26 PM
I'm an idiot, I updated the default value in the script, but the old placeholder was still in the UI field in Unity. I updated the URL in Unity and it worked as expected.
Answer by Llama_w_2Ls · Jul 30, 2020 at 11:52 AM
public InputField inputField; //The URL I want to post to
public InputField FormData; //What to post
IEnumerator Upload()
{
WWWForm form = new WWWForm();
form.AddField("", FormData.text);
using (UnityWebRequest www = UnityWebRequest.Post(inputField.text, form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
text.text = ByteArrayToString(form.data); //A text UI that displays the data
//Or you could do: Debug.Log(ByteArrayToString(form.data));
string ByteArrayToString(byte[] val) //Converts the data to ascii
{
string b = "";
int len = val.Length;
for (int i = 0; i < len; i++)
{
b += Convert.ToChar(val[i]);
}
return b;
}
}
}
}
Heres how I posted using UnityWebRequest, with two inputfields for the data I want to post and the URL to post to
Your answer
Follow this Question
Related Questions
Try catch alternative for HTTP request 1 Answer
Using async WebRequest GetResponse 1 Answer
How to Upload multiple files to a server using UnityWebRequest.Post(); 3 Answers
Does overriding UnityWWWRequestDefaultProvider works for UnityWebRequest? 0 Answers
Unity Web Request getting response 0 Answers