Quantcast
Channel: Questions in topic: "webrequest"
Viewing all articles
Browse latest Browse all 387

POST to HTTP not working

$
0
0
I've been trying to get [this tutorial][1] 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) 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. [1]: https://youtu.be/4W90-mh70JY

Viewing all articles
Browse latest Browse all 387

Trending Articles