Home > Mobile >  Unity http WebRequest post/send data to a input-field
Unity http WebRequest post/send data to a input-field

Time:11-18

I want to use UnityWebRequest to post data into an input field on a website for authorization. I am able to post data to a website called "https://httpbin.org/post" and I got a success message beeing able to post data to a website:

Success {
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "data": "LOL"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "deflate, gzip", 
    "Content-Length": "8", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "UnityPlayer/2021.3.11f1 (UnityWebRequest/1.0, libcurl/7.84.0-DEV)", 
    "X-Amzn-Trace-Id": "Root=1-63753ab1-7eb673a229988fc954b32ae8", 
    "X-Unity-Version": "2021.3.11f1"
  }, 
  "json": null, 
  "origin": "31.18.250.181", 
  "url": "https://httpbin.org/post"
}

but this is just posting data into nothing and I want to post data into an input field like this:

<input type="text" name="_username">

It is for authorization with username and password and later I need to get the text data of a redirect site after logging in.

This is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Exception = System.Exception;

public class TestWebRequest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        try
        {
            string url = "www.ling.com";

            WWWForm form = new WWWForm();
            form.AddField("_username", "test");
            var request = UnityWebRequest.Post(url, form);
            //request.SetRequestHeader("Content-Type", "application/json");
            //request.SetRequestHeader("Accept", "text/csv");
            //request.SetRequestHeader("appKey", "ABC");
            StartCoroutine(onResponse(request));
        }
        catch (Exception e) { Debug.Log("ERROR : "   e.Message); }
    }

    private IEnumerator onResponse(UnityWebRequest req)
    {

        yield return req.SendWebRequest();
        if (req.isNetworkError)
          Debug.Log("Network error has occured: "   req.GetResponseHeader(""));
        else
            Debug.Log("Success " req.downloadHandler.text );
            byte[] results = req.downloadHandler.data;
        Debug.Log("Second Success");
        // Some code after success

        req.Dispose();

    }
}

I can't show the exact link but as I said it has two input fileds one password and one username input field that need to be filled out for authorization and after that I need to submit the form to get redirected were I then want to get the text data from which works with get. I don't know if this is the best way of doing this but I need to access text data on the website that you have to be logged into and it can't be done with cookies (I think) because it are different credentials every time.

Thank you so much for helping!

CodePudding user response:

When you're submitting data, you're actually filling out a form. And then you submit that form. What you need to understand first is that your app does not interact with a website's frontend, it only interacts with the form at hand.

So, say if the website is a PHP website, that would have some logic at some point that gets a user parameter and a password parameter as GET parameters, and would do something based on those.

I cannot recommend this tutorial series enough for what you're trying to do.

Secondly, please do not send passwords over Unity's Web Request utility as plaintext to any website. It is not secure at all. That is a very sensitive subject, you can start off by hashing every password you get as soon as you get it from Unity, and you may submit the hashed form to the database.

And for the last part, the best way of doing this is probably using an API rather than a simple PHP website like the tutorial above.

  • Related