Is there another way to send an HTTP request without using Application.OpenURL? This is my C# code, but it opens the request in a new tab of the default browser (which I do not want). I just want the request sent without opening any browser. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class openWebPage : MonoBehaviour { // Use this for initialization void Start () { Application.OpenURL ("http://myusername:mypassword@mywebsite/command"); } // Update is called once per frame void Update () { } }
From the Application.OpenURL documentation: So that's working as designed. But it sounds like you're looking for UnityWebRequest.
Thank you for the link. I've changed my code to this (see below) and I receive no errors but it still doesn't send the http request: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class openWebPage : MonoBehaviour { // Use this for initialization void Start () { UnityWebRequest wr = new UnityWebRequest("http://myusername:mypassword@mywebsite/command"); } // Update is called once per frame void Update () { } }
You really need to go look at the documentation. There is example code showing exactly what you need to do for various scenarios. What you have so far isn't quite enough.
Thank you for the response. I've been scavenging through the example code and trying all sorts of things but my knowledge is limited so I was hoping someone here could point out what I'm missing.
Here is the code example from the documentation, assuming you're trying to "get" data. Code (CSharp): class MyBehaviour: MonoBehaviour { void Start() { StartCoroutine(GetText()); } IEnumerator GetText() { UnityWebRequest www = UnityWebRequest.Get("http://www.my-server.com"); yield return www.Send(); if(www.isError) { Debug.Log(www.error); } else { // Show results as text Debug.Log(www.downloadHandler.text); // Or retrieve results as binary data byte[] results = www.downloadHandler.data; } } }
Wow this worked! I just had to change the word "MyBehavior" to match the name of my script. Thank you for sending this!
So...this script is great (in the editor) and when published to PC/MAC but it doesn't do anything when published to Android. I want to try and use the same script but in Javascript, however, the Unity manual has no documentation example for the Javascript version. Any thoughts?
You can't code in Javascript using Unity. I assume you mean UnityScript? That language is pretty much on the way out and quite likely to be unsupported in the future, which explains the lack of documentation in that language. You'll need to figure out the minor syntax differences to convert the script, or maybe someone here who remembers the syntax can help you out. Otherwise Google is your friend. On Android I there's likely some runtime error. You should be able to look at the app's log in some way - perhaps compile a development version and you can see the log in the editor console? It's been too long since I've developed on mobile.
Thank you for keeping this conversation going with me. I appreciate it. I didn't realize you couldn't code in Javascript using Unity. In Unity, when I go under the "Assets --> Create" dropdown, I see two options for creating scripts: C# Script and Javascript. I figured you could use either, so I just started with C#. However, it makes sense that you are saying Javascript is not supported (or will not be supported) because I can't find any documentation on the language for Unity. I have been looking at the log in the editor console in the development version and I get no errors, which is why this is even more confusing. I'm beginning to think it's just not worth it to make an app for mobile devices, since a basic HTTP request can't even be fired. It's not like I'm trying to do something fancy. Quite frustrating that I'll be limited to publishing for PC/MAC since mobile has so many roadblocks.
They call it Javascript, but it isn't Javascript. It was made to seem similar, but it's a very different language. You're best off sticking with C#. As far as the web request, you probably just have some setup issue. Perhaps add your own logging to your test app. And welcome to software development. Things don't always go smoothly and you have to work through issues. This may be your first roadblock, but there won't ever be a last one.
Good advice. Thank you for all your help and support. I'll keep working on it and see if I can find a solution, and when I do, I'll post it here.
I've figured out the solution! Mobile devices don't allow you to send an http request with a username and a password in it (for security purposes). Instead, you have to pass an authentication string and then send the http request. Here's my code that finally worked on Android: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class OpenWebPage: MonoBehaviour { void Start() { StartCoroutine(GetText()); } string authenticate(string username, string password) { string auth = username + ":" + password; auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth)); auth = "Basic " + auth; return auth; } IEnumerator GetText() { string authorization = authenticate("InsertUserNameHere", "InsertPasswordHere"); while (true) { string url = "http://insertWebAddressHere/optionalCommand"; UnityWebRequest www = UnityWebRequest.Get(url); www.SetRequestHeader("AUTHORIZATION", authorization); yield return www.Send(); } } } Remember to change the name of the public class to be the same name as your script. In my case, I called my script OpenWebPage so I named my class OpenWebPage.
Glad you got it working! This same code should work for PC as well. This is a bit nitpicky, but the standard for class names is to begin with upper case, so OpenWebPage instead of openWebPage. You can of course use whatever standard you want
Thank you for the information. I'd like to use what is considered the "Standard" so I'll update my script to use Upper Case for the first letter. Thank you again Dave-Carlile. Couldn't have done it without your help.