【问题标题】:Unity http WebRequest post/send data to a input-fieldUnity http WebRequest 将数据发布/发送到输入字段
【发布时间】:2022-11-17 07:44:41
【问题描述】:

我想使用 UnityWebRequest 将数据发布到网站的输入字段中以获得授权。 我能够将数据发布到一个名为“https://httpbin.org/post”的网站,并且我收到了一条成功消息,提示我能够将数据发布到一个网站:

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"
}

但这只是将数据发布到任何内容中,我想将数据发布到这样的输入字段中:

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

它用于使用用户名和密码进行授权,稍后我需要在登录后获取重定向站点的文本数据。

这是代码:

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();

    }
}

我无法显示确切的链接,但正如我所说,它有两个输入字段,一个密码和一个用户名输入字段,需要填写以获得授权,之后我需要提交表单以进行重定向,然后我想得到与 get 一起使用的文本数据。我不知道这是否是最好的方法,但我需要访问您必须登录的网站上的文本数据,并且不能使用 cookie 来完成(我认为),因为它是不同的凭据时间。

非常感谢您的帮助!

【问题讨论】:

    标签: unity3d http c#-4.0 httpwebrequest


    【解决方案1】:

    当您提交数据时,实际上是在填写表格。然后你提交那个表格。您首先需要了解的是,您的应用程序不与网站的前端交互,它只与手边的表单交互。

    因此,假设该网站是一个 PHP 网站,那么在某些时候会有一些逻辑将用户参数和密码参数作为 GET 参数获取,并根据这些逻辑执行某些操作。

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

    其次,请不要通过 Unity 的 Web Request 实用程序将密码作为明文发送到任何网站。它根本不安全。这是一个非常敏感的主题,您可以先对从 Unity 获得的每个密码进行哈希处理,然后将哈希处理后的表格提交到数据库。

    对于最后一部分,执行此操作的最佳方法可能是使用 API,而不是像上面教程那样使用简单的 PHP 网站。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      相关资源
      最近更新 更多