使用Unity在C#中发送http请求 [英] Sending http requests in C# with Unity
问题描述
如何使用Unity在C#中发送HTTP GET和POST请求?
How can I send HTTP GET and POST requests in C# with Unity?
我想要的是:
- 在post请求中发送json数据(我使用Unity序列化器,因此不需要
新的一个,我只想在post数据中传递字符串并且能够设置
ContentType to application / json); - 获取响应代码和正文没有任何问题;
- 在不阻止ui渲染的情况下完成所有异步操作。
我尝试了什么:
- 实施HttpWebRequest / HttpWebResponse,但它太硬和低级别(如果我找不到更好的东西,我将不得不使用它);
- 使用统一WWW,但它没有符合我的要求;
- 使用NuGet的一些外部包 - Unity不接受它们:(
大多数问题都是使用线程,我在C#中没有足够的经验。
我使用的文本编辑器是Intellij Rider。
Most problems were with threading, I'm not experienced enough in it in C#. Text editor, 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.
注意:
对于以下示例,如果您使用 Unity 2017.2 下面的任何内容,请将 SendWebRequest()
替换为发送()
然后用 isError
替换 isNetworkError
。这将适用于较低版本的Unity。此外,如果您需要以二进制形式访问下载的数据,请使用 uwr.downloadHandler.data $替换
uwr.downloadHandler.text
C $ C>。最后, SetRequestHeader
函数用于设置请求的标头。
For the examples below, if you are using anything below Unity 2017.2, replace SendWebRequest()
with Send()
and then replace isNetworkError
with isError
. This will then work for the lower version of Unity. Also, if you need to access the downloaded data in a binary form instead, replace uwr.downloadHandler.text
with uwr.downloadHandler.data
. Finally, the SetRequestHeader
function is used to set the header of the request.
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);
}
}
使用Multipart FormData / Multipart表单文件的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请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!