title: A method for making HTTP requests on Unity iOS?
body: <p>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,</p>
  
  <h1>WebRequest.HttpWebRequest</h1>
  
  <p>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.</p>
  
  <h1>UnityEngine.WWW#</h1>
  
  <p>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.</p>
  
  <h1>System.Threading</h1>
  
  <p>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).</p>
  
  <h1>Run threads in a native iOS app with a bridge to the Unity app</h1>
  
  <p>Ridiculous, not even going to attempt this.</p>
  
  <h1>UniWeb</h1>
  
  <p>This. I would like to know how they managed it.</p>
  
  <p>Here is an example of the WebRequest.BeginGetResponse method I am trying,</p>
  
  <pre><code>// 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);
      }
  }
  </code></pre>
  
  <p>... based on this: <a href="http://msdn.microsoft.com/en-us/library/86wf6409(v=vs.71).aspx">http://msdn.microsoft.com/en-us/library/86wf6409(v=vs.71).aspx</a></p>