我想使用 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 來完成(我認為),因為它是不同的憑據時間。
非常感謝您的幫助!
uj5u.com熱心網友回復:
當您提交資料時,實際上是在填寫表格。然后你提交那個表格。您首先需要了解的是,您的應用程式不與網站的前端互動,它只與手邊的表單互動。
因此,假設該網站是一個 PHP 網站,那么在某些時候會有一些邏輯將用戶引數和密碼引數作為GET引數,并根據這些邏輯執行某些操作。
對于您想要做的事情,我強烈推薦本教程系列。
其次,請不要通過 Unity 的 Web Request 實用程式將密碼作為明文發送到任何網站。它根本不安全。這是一個非常敏感的主題,您可以先對從 Unity 獲得的每個密碼進行哈希處理,然后將哈希處理后的表格提交到資料庫。
對于最后一部分,執行此操作的最佳方法可能是使用 API 而不是像上面教程那樣的簡單 PHP 網站。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/536478.html
上一篇:當玩家碰撞時使物體搖晃
