在Unity上使用.NET自己的httpClient类 [英] Use .NET's own httpClient class on Unity

查看:818
本文介绍了在Unity上使用.NET自己的httpClient类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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及更高版本,则在UnityEngine.Networking;名称空间中可以找到UnityWebRequest.

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,UnityWebRequestcan 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.

启用后, 转到<UnityInstallationDirectory>\Editor\Data\MonoBleedingEdge\lib\mono\4.5或例如我的计算机上的C:\Program Files\Unity\Editor\Data\MonoBleedingEdge\lib\mono\4.5.

After enabling it, Go to <UnityInstallationDirectory>\Editor\Data\MonoBleedingEdge\lib\mono\4.5 or for example, C:\Program Files\Unity\Editor\Data\MonoBleedingEdge\lib\mono\4.5 on my computer.

在此目录中,一旦将System.Net.Http.dll复制到您的<ProjectName>\Assets目录中,并且在导入System.Net.Http名称空间之后,您应该能够使用HttpClient.如果存在有关缺少依赖项的其他错误,您也可以从此路径中获取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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆