在 Unity 上使用 .NET 自己的 httpClient 类 [英] Use .NET's own httpClient class on Unity
问题描述
我正在尝试从 Unity 执行 HTTP 删除请求,并且想到了使用 .Net 的 System.Web 命名空间中包含的 HttpRequest 类的想法
I'm trying to do a HTTP Delete request from Unity and bump into the idea of use the HttpRequest class included in the System.Web namespace of .Net
我怎样才能做到这一点,我假设必须对该命名空间进行某种导入,但是如何实现?
How can I achieve this, I supose that some sort of import of that namespace must be done, but how?
希望有人能给我一些指导
Hope some one can give me some orientation
推荐答案
HttpClient
仅在 4.5 NET 及更高版本中可用,Unity 不使用该版本.Unity 使用大约 3.5 .NET 版本.
HttpClient
is only available in 4.5 NET and above and Unity does not use that version. Unity uses about 3.5 .NET version.
如果您使用的是 Unity 5.3,UnityWebRequest.Delete
可用于发出删除请求.它可以在 Experimental.Networking
命名空间中找到.如果您使用的是 Unity 5.4 及更高版本,UnityWebRequest
可以在 UnityEngine.Networking;
命名空间中找到.
If you are using Unity 5.3, UnityWebRequest.Delete
can be used to make a Delete request. It can be found in the Experimental.Networking
namespace. If you are using Unity 5.4 and above,UnityWebRequest
can be found in the UnityEngine.Networking;
namespace.
完整的工作示例:
IEnumerator makeRequest(string url)
{
UnityWebRequest delReq = UnityWebRequest.Delete(url);
yield return delReq.Send();
if (delReq.isError)
{
Debug.Log("Error: " + delReq.error);
}
else
{
Debug.Log("Received " + delReq.downloadHandler.text);
}
}
用法:
StartCoroutine(makeRequest("http://www.example.com/whatever"));
确保包含 using UnityEngine.Networking
.您可以在此处找到完整的示例.
Make sure to include using UnityEngine.Networking
. You can find complete examples with it here.
编辑(更新)
Unity 现在支持 .NET 4.5,因此您现在可以根据需要使用 HttpClient.请参阅这篇帖子了解如何启用它.
Unity now supports .NET 4.5 so you can now use HttpClient if you wish. See this post for how to enable it.
启用后,转到
或例如 C:Program FilesUnityEditorDataMonoBleedingEdgelibmono4.5
在我的电脑上.
After enabling it,
Go to <UnityInstallationDirectory>EditorDataMonoBleedingEdgelibmono4.5
or for example, C:Program FilesUnityEditorDataMonoBleedingEdgelibmono4.5
on my computer.
进入此目录后,将 System.Net.Http.dll
复制到您的
目录,您应该可以使用 HttpClient
导入 System.Net.Http
命名空间后.如果还有其他关于缺少依赖项的错误,您也可以从该路径获取 dll,并将它们也复制到您的 <ProjectName>Assets
目录中.
Once in this directory, copy System.Net.Http.dll
to your <ProjectName>Assets
directory and you should be able to use HttpClient
after importing the System.Net.Http
namespace. If there are some other error about missing dependencies, you can get the dlls from this path too and copy them to your <ProjectName>Assets
directory too.
这篇关于在 Unity 上使用 .NET 自己的 httpClient 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!