【发布时间】: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