Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Send HTTP Request

Discussion in 'Scripting' started by Velketor, Jul 19, 2017.

  1. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class openWebPage : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         Application.OpenURL ("http://myusername:mypassword@mywebsite/command");
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.        
    16.     }
    17. }
    18.  
     
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    From the Application.OpenURL documentation:

    So that's working as designed. But it sounds like you're looking for UnityWebRequest.
     
    Velketor likes this.
  3. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class openWebPage : MonoBehaviour
    7. {
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         UnityWebRequest wr = new UnityWebRequest("http://myusername:mypassword@mywebsite/command");
    12.  
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.        
    18.     }
    19. }
     
  4. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    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.
     
    Velketor likes this.
  5. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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.
     
  6. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Here is the code example from the documentation, assuming you're trying to "get" data.

    Code (CSharp):
    1. class MyBehaviour: MonoBehaviour {
    2.     void Start() {
    3.         StartCoroutine(GetText());
    4.     }
    5.     IEnumerator GetText() {
    6.         UnityWebRequest www = UnityWebRequest.Get("http://www.my-server.com");
    7.         yield return www.Send();
    8.         if(www.isError) {
    9.             Debug.Log(www.error);
    10.         }
    11.         else {
    12.             // Show results as text
    13.             Debug.Log(www.downloadHandler.text);
    14.             // Or retrieve results as binary data
    15.             byte[] results = www.downloadHandler.data;
    16.         }
    17.     }
    18. }
     
    Velketor likes this.
  7. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Wow this worked! I just had to change the word "MyBehavior" to match the name of my script. Thank you for sending this!
     
  8. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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?
     
  9. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    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.
     
    Velketor likes this.
  10. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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.
     
  11. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    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.
     
    Velketor likes this.
  12. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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.
     
    Dave-Carlile likes this.
  13. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class OpenWebPage: MonoBehaviour {
    7.             void Start()
    8.             {
    9.                 StartCoroutine(GetText());
    10.             }
    11.             string authenticate(string username, string password)
    12.             {
    13.                 string auth = username + ":" + password;
    14.                 auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
    15.                 auth = "Basic " + auth;
    16.                 return auth;
    17.             }
    18.             IEnumerator GetText()
    19.             {
    20.                 string authorization = authenticate("InsertUserNameHere", "InsertPasswordHere");
    21.                 while (true)
    22.                 {
    23.                     string url = "http://insertWebAddressHere/optionalCommand";
    24.                     UnityWebRequest www = UnityWebRequest.Get(url);
    25.                     www.SetRequestHeader("AUTHORIZATION", authorization);
    26.                     yield return www.Send();
    27.         }
    28.     }
    29. }
    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.
     
    Last edited: Jul 20, 2017
  14. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    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 :)
     
    Velketor likes this.
  15. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    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.
     
    Dave-Carlile likes this.