相关疑难解决方法(0)

使用Unity在C#中发送http请求

如何使用Unity在C#中发送HTTP GET和POST请求?

我想要的是:

  • 在post请求中发送json数据(我使用Unity序列化器,因此不需要新的,我只想在post数据中传递字符串并且能够将ContentType设置为application/json);
  • 获得响应代码和正文没有任何问题;
  • 在不阻止ui渲染的情况下完成异步操作.

我尝试过的:

  • 使用HttpWebRequest/HttpWebResponse实现,但它太难和低级别(如果我找不到更好的东西,我将不得不使用它);
  • 使用统一WWW,但它不符合我的要求;
  • 使用NuGet的一些外部包 - Unity不接受它们:(

大多数问题都是使用线程,我在C#中没有足够的经验.我使用的文本编辑器是Intellij Rider.

c# http unity-game-engine

24
推荐指数
1
解决办法
2万
查看次数

在Unity iOS上发出HTTP请求的方法?

我需要使用所有标准RESTful方法发送HTTP请求并访问请求正文以便使用它发送/接收JSON.我调查过,

WebRequest.HttpWebRequest

这几乎是完美的,但有些情况下,例如,如果服务器关闭,函数GetResponse可能需要几秒钟才能返回 - 因为它是一个同步方法 - 冻结该时段的应用程序.这个方法的异步版本,BeginGetResponse,似乎不是异步工作(在Unity中),因为它仍然冻结该时期的应用程序.

UnityEngine.WWW#

由于某些原因,只支持POST和GET请求 - 但我还需要PUT和DELETE(标准的RESTful方法),所以我没有进一步深入研究它.

的System.Threading

为了运行WebRequest.HttpWebRequest.GetResponse而不冻结应用程序,我研究了使用线程.线程似乎在编辑器中工作(但看起来非常不稳定 - 如果你在应用程序退出时不停止线程,即使你停止它也会永远在编辑器中运行),并且当构建到iOS设备时它会立即崩溃因为我尝试启动一个线程(我忘记写下错误,我现在无法访问它).

在本机iOS应用程序中运行线程,并使用Unity应用程序的桥接器

荒谬,甚至不会尝试这个.

UniWeb

这个.我想知道他们是如何管理它的.

这是我正在尝试的WebRequest.BeginGetResponse方法的示例,

// The RequestState class passes data across async calls.
public class RequestState
{
   const int BufferSize = 1024;
   public StringBuilder RequestData;
   public byte[] BufferRead;
   public WebRequest Request;
   public Stream ResponseStream;
   // Create Decoder for appropriate enconding type.
   public Decoder StreamDecode = Encoding.UTF8.GetDecoder();

   public RequestState()
   {
      BufferRead = new byte[BufferSize];
      RequestData = new StringBuilder(String.Empty);
      Request = null;
      ResponseStream = null;
   } …
Run Code Online (Sandbox Code Playgroud)

c# multithreading http unity-game-engine ios

20
推荐指数
2
解决办法
3万
查看次数

UnityWebRequest.downloadHandler返回null,同时从Node Server获得响应

我正在Unity上创建一个注册场景.在后端我有MongoDB的NodeJS服务器.注册成功,数据也保存在Mongo上.

这是我的NodeJS api用于注册

api.post('/register', (req,res) => {
Account.register(new Account({username: req.body.username}), req.body.password, function(err, account){
  console.log("acc: "+account);
  if(err){
    if (err.name == "UserExistsError") {
      console.log("User Exists");
      return res.status(409).send(err);
    }else {
      console.log("User Error 500");
      return res.status(500).send(err);
    }
  }else {
    let newUser = new User();
    newUser.accountid = account._id;
    newUser.name = req.body.fullname;
    newUser.gender = req.body.gender;
    newUser.role = req.body.role;
    newUser.country = req.body.country;
    newUser.coins = req.body.coins;
    newUser.save(err => {
      if(err){
        console.log(err);
        return res.send(err);
      }else{
        console.log('user saved');
        res.json({ message: 'User saved' });
      }
    });
    passport.authenticate(
      'local', {
        session: …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine mongodb node.js unity5

7
推荐指数
1
解决办法
5800
查看次数

Unity:在Unity3D中使用HTTP PUT

我是Unity的新手,在Unity中遇到RESTFul的一些问题.我想使用HTTP PUT更新服务器上的一些数据,但正如我在搜索网络时收到的那样,Unity中的WWWW类不支持HTTP PUT.我还尝试了一些与HTTP PUT相关的HttpWebRequest示例,但总是收到错误代码400:错误请求.

我怎么解决这个问题?我是否必须在更新时列出所有键值对,或者只需要列出我想要更改值的对?

rest http put unity-game-engine

6
推荐指数
2
解决办法
5956
查看次数

标签 统计

unity-game-engine ×4

c# ×3

http ×3

ios ×1

mongodb ×1

multithreading ×1

node.js ×1

put ×1

rest ×1

unity5 ×1