问题描述
如何使用 Unity 在 C# 中发送 HTTP GET 和 POST 请求?
How can I send HTTP GET and POST requests in C# with Unity?
我想要的是:
- 在 post 请求中发送 json 数据(我使用 Unity 序列化程序,所以不需要新的,我只想在帖子数据中传递字符串并且有能力将 ContentType 设置为 application/json);
- 毫无问题地获取响应代码和正文;
- 在不阻塞 ui 渲染的情况下异步执行所有操作.
我尝试过的:
- 使用 HttpWebRequest/HttpWebResponse 实现,但它太难而且太低级(如果我找不到更好的东西,我将不得不使用它);
- 使用 unity WWW,但它不符合我的要求;
- 使用来自 NuGet 的一些外部包 - Unity 不接受它们 :(
大多数问题都与线程有关,我在 C# 方面经验不足.我使用的 IDE 是 Intellij Rider.
Most problems were with threading, I'm not experienced enough in it in C#.IDE, I use, is Intellij Rider.
推荐答案
WWW
API 应该可以完成这项工作,但是 UnityWebRequest
替换了它,所以我会回答更新的 API.这真的很简单.您必须使用协程通过 Unity 的 API 来执行此操作,否则您必须使用 C# 标准 Web 请求 API 和线程之一.使用协程,您可以在请求完成之前让出请求.这不会阻塞主线程或阻止其他脚本运行.
The WWW
API should get this done but UnityWebRequest
replaced it so I will answer the newer API. It's really simple. You have to use coroutine to do this with Unity's API otherwise you have have to use one of C# standard web request API and Thread. With coroutine you can yield the request until it is done. This will not block the main Thread or prevent other scripts from running.
注意:
GET 请求:
void Start()
{
StartCoroutine(getRequest("http:///www.yoururl.com"));
}
IEnumerator GetRequest(string uri)
{
UnityWebRequest uwr = UnityWebRequest.Get(uri);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
}
使用表单的 POST 请求:
void Start()
{
StartCoroutine(postRequest("http:///www.yoururl.com"));
}
IEnumerator PostRequest(string url)
{
WWWForm form = new WWWForm();
form.AddField("myField", "myData");
form.AddField("Game Name", "Mario Kart");
UnityWebRequest uwr = UnityWebRequest.Post(url, form);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
}
使用 Json 的 POST 请求:
void Start()
{
StartCoroutine(postRequest("http:///www.yoururl.com", "your json"));
}
IEnumerator PostRequest(string url, string json)
{
var uwr = new UnityWebRequest(url, "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
uwr.SetRequestHeader("Content-Type", "application/json");
//Send the request then wait here until it returns
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
}
带有多部分表单数据/多部分表单文件的 POST 请求:
void Start()
{
StartCoroutine(postRequest("http:///www.yoururl.com"));
}
IEnumerator PostRequest(string url)
{
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));
UnityWebRequest uwr = UnityWebRequest.Post(url, formData);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
}
PUT 请求:
void Start()
{
StartCoroutine(putRequest("http:///www.yoururl.com"));
}
IEnumerator PutRequest(string url)
{
byte[] dataToPut = System.Text.Encoding.UTF8.GetBytes("Hello, This is a test");
UnityWebRequest uwr = UnityWebRequest.Put(url, dataToPut);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
}
删除请求:
void Start()
{
StartCoroutine(deleteRequest("http:///www.yoururl.com"));
}
IEnumerator DeleteRequest(string url)
{
UnityWebRequest uwr = UnityWebRequest.Delete(url);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Deleted");
}
}
这篇关于使用 Unity 在 C# 中发送 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!