Giter Site home page Giter Site logo

imclab / unity-urlclient Goto Github PK

View Code? Open in Web Editor NEW

This project forked from imkira/unity-urlclient

0.0 1.0 0.0 232 KB

HTTP/HTTPS client for Unity (iOS/Android/MacOSX)

License: MIT License

Ruby 1.33% Java 22.45% C# 39.43% Python 17.02% Objective-C 15.30% Objective-C++ 4.46%

unity-urlclient's Introduction

unity-urlclient

unity-urlclient is a Unity 3D plugin for downloading files via HTTP/HTTPS.

Requirements

  • Unity 3.4.x Pro and above.

Features

  • Multi-platform: iOS/Android/MacOSX support.
  • Protocol: HTTP/HTTPS
  • Receiving Data: Stream, MemoryStream, byte array, low-overhead direct download and storage (resume support).
  • Sending Data: byte array, low-overhead direct upload support.
  • Settings: HTTP Header, Query String, Cookie, Post Body
  • Advanced: Progress, Cache, Redirect, Timeout

Roadmap

  • Upload with Stream support
  • Cookie Storage

Download & Installation

Latest version: v1.0.0

Unity 3.x

Unity 4.x

How To Use

Simple Request

using (URLClient.HTTPClient client = new URLClient.HTTPClient("unity3d.com"))
{
  yield return StartCoroutine(client.WaitUntilDone());

  ///
  // check response
  // ...
}

Advanced Request

You can create a request object with:

URLClient.HTTPRequest request = new URLClient.HTTPRequest();

Then you customize the request like you want like (check API here)

request.SetURL("http://www.google.com").
  AppendQueryParameter("q", "unity").
  SetHeader("User-Agent", "dummy user agent").
  AppendCookie("name", "value").
  Get();

Then you create a URLClient with:

using (URLClient.HTTPClient client = new URLClient.HTTPClient(request))
{
  yield return StartCoroutine(client.WaitUntilDone());

  ///
  // check response
  // ...
}

Checking Response

You can check the response any time during or after you create the client. It may return null in case there is no response.

You can get the response with:

if (client.Response == null)
{
  // no response yet
}
else
{
  // commonly used fields:
  // client.Response.StatusCode;
  // client.Response.ContentToBytes();
  // client.Response.ContentToString();
  // client.Response.Progress;
  // etc.
}

client.Response follows the URLClient.IHTTPResponse interface:

public interface IHTTPResponse
{
  long StatusCode { get; }
  IHTTPHeaderList Headers { get; }

  ulong ExpectedReceiveContentLength { get; }
  ulong ResumedContentLength { get; }
  ulong ReceivedContentLength { get; }
  ulong ExpectedAcquiredContentLength { get; }
  ulong AcquiredContentLength { get; }

  bool SupportsContentStream { get; }
  Stream ContentStream { get; }

  bool SupportsContentMemoryStream { get; }
  MemoryStream ContentMemoryStream { get; }

  bool SupportsContentFilePath { get; }
  string ContentFilePath { get; }

  byte[] ContentToBytes();
  string ContentToString(Encoding enc = null);

  int RedirectCount { get; }

  bool IsProgressAvailable { get; }
  float Progress { get; }
  float ReceiveProgress { get; }
}

Error Handling

You can check for error at any time during or after you create the client.

if (client.Error != null)
{
  // client.Error.Domain
  // client.Error.Code
  // client.Error.Description
}

Cancelling Client

You can cancel a running client at any time with:

client.Cancel();

Coroutines

You don't have to use coroutines. You can create and run a client with:

URLClient.HTTPClient client = new URLClient.HTTPClient(...);

Then, instead of yield return StartCoroutine(client.WaitUntilDone()); you can call the following at any point in time:

if (client.IsDone)
{
  // client finished!
  // check client.Response
}
else
{
  if (client.Response == null)
  {
    // no response yet
  }
  else
  {
    // check progress with client.Response.Progress
    // etc.
  }
}

However, you must destroy the client when you don't need it (otherwise you will get memory leaks):

client.Dispose();
client = null;

Demonstration

unity/Assets/URLClient/Demo/URLClientDemo.cs

Contribute

  • Found a bug?
  • Want to contribute and add a new feature?

Please fork this project and send me a pull request!

License

unity-urlclient is licensed under the MIT license:

www.opensource.org/licenses/MIT

Copyright

Copyright (c) 2013 Mario Freitas. See LICENSE.txt for further details.

unity-urlclient's People

Contributors

imkira avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.