Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 30, 2020 at 05:27 PM by MatchaJoeJoe for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MatchaJoeJoe · Jul 30, 2020 at 06:24 AM · wwwwebwebrequestposthttpwebrequest

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges