一种在 Unity iOS 上发出 HTTP 请求的方法? [英] A method for making HTTP requests on Unity iOS?

查看:20
本文介绍了一种在 Unity iOS 上发出 HTTP 请求的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to send HTTP requests with all the standard RESTful methods and access to the body of the request in order to send/receive JSON with it. I've looked into,

这几乎可以完美运行,但在某些情况下,例如,如果服务器关闭,函数 GetResponse 可能需要几秒钟才能返回 - 因为它是一种同步方法 - 在那段时间内冻结应用程序.此方法的异步版本 BeginGetResponse 似乎无法异步工作(无论如何在 Unity 中),因为它在此期间仍会冻结应用程序.

This works almost perfectly, but there are cases where, for example, if the server is down the function GetResponse can take several seconds to return- since it is a synchronous method- freezing the application for that period. The asynchronous version of this method, BeginGetResponse, does not seem to work asynchronously (in Unity anyway) as it still freezes the application for that period.

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

Only supports POST and GET requests for some reason- but I also need PUT and DELETE (standard RESTful methods) so I didn't bother looking into it any further.

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

In order to run WebRequest.HttpWebRequest.GetResponse without freezing the application I looked into using threads. Threads seem to work in the editor (but seem extremely volatile- if you don't stop a thread when the application exits it keeps running in the editor forever even when you stop it), and when built to an iOS device crash it as soon as I try to start a thread (I forgot to write down the error and I don't have access to it right now).

荒谬,甚至不打算尝试这个.

Ridiculous, not even going to attempt this.

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

This. I would like to know how they managed it.

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

Here is an example of the WebRequest.BeginGetResponse method I am trying,

// 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;
   }     
}

public class WebRequester
{
    private void ExecuteRequest()
    {
        RequestState requestState = new RequestState();
        WebRequest request = WebRequest.Create("mysite");
        request.BeginGetResponse(new AsyncCallback(Callback), requestState);
    }

    private void Callback(IAsyncResult ar)
    {
      // Get the RequestState object from the async result.
      RequestState rs = (RequestState) ar.AsyncState;

      // Get the WebRequest from RequestState.
      WebRequest req = rs.Request;

      // Call EndGetResponse, which produces the WebResponse object
      //  that came from the request issued above.
      WebResponse resp = req.EndGetResponse(ar);
    }
}

...基于此:http://msdn.microsoft.com/en-us/library/86wf6409(v=vs.71).aspx

推荐答案

我在 iOS 上使用了线程 - 我相信它是由于幽灵线程或其他原因而崩溃的.重新启动设备似乎已经解决了崩溃问题,所以我将只使用 WebRequest.HttpWebRequest 和线程.

I got threading to work on iOS- I believe it was crashing due to ghost threads or something. Restarting the device seems to have fixed the crashing so I'll just use WebRequest.HttpWebRequest with threads.

这篇关于一种在 Unity iOS 上发出 HTTP 请求的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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