一种用于iOS的统一制作的HTTP请求的方法? [英] A method for making HTTP requests on Unity iOS?

查看:160
本文介绍了一种用于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。线程似乎在编辑器中工作(而显得格外volatile-如果应用程序退出时它一直在编辑器中运行,即使永远当你停止它不停止线程),当尽快建成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 0.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.

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

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