Giter Site home page Giter Site logo

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.1

Unity 3.x

Unity 4.x / Unity 5.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 jbroadway avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unity-urlclient's Issues

DllNotFoundException: URLClient

Hello,

I've created a new project on Unity 5.3.4f1 personal and I've imported your 2 packages (plugin and demo). When I run the demo I have this error and the following trace :

DllNotFoundException: URLClient
URLClient.HTTPClient.Start () (at Assets/Plugins/URLClient/http/HTTPClient.cs:212)
URLClient.HTTPClient.get_IsDone () (at Assets/Plugins/URLClient/http/HTTPClient.cs:129)
URLClient.HTTPClient+c__Iterator0.MoveNext () (at Assets/Plugins/URLClient/http/HTTPClient.cs:323)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
c__Iterator0:MoveNext() (at Assets/URLClient/Demo/URLClientDemo.cs:53)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
c__IteratorE:MoveNext() (at Assets/URLClient/Demo/URLClientDemo.cs:433)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
URLClientDemo:OnGUI() (at Assets/URLClient/Demo/URLClientDemo.cs:470)

Do I miss something in the importation/project setting (sorry I'm a little newbie with unity) ??

Thanks for your help
Antoine

Calling Method from Android Build via Reflection Throwing Error

Unity 5.3.4

When the disposable object is created and the plugin is called, it fails on calling the plugin methods via reflection.

D/Unity (19142): ? getMethodID("com.github.imkira.unityurlclient.UnityURLClientBinding", "createHTTPConnection", "(Ljava/lang/String;Ljava/lang/String;IF)I", non-static)
D/Unity (19142): ! int UnityURLClientBinding.createHTTPConnection(String, String, int, float);
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_FromReflectedMethod()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_NewStringUTF()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_NewStringUTF()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_CallIntMethod(1e20057a, 57e12c08
D/Unity (19142): > , ff800005
D/Unity (19142): > , 50500009
D/Unity (19142): > , 00000000
D/Unity (19142): > , 42700000
D/Unity (19142): > )
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_NewStringUTF()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_NewStringUTF()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_CallStaticObjectMethod(1d200522, 57ddf2a8
D/Unity (19142): > , 21800572
D/Unity (19142): > , ff900005
D/Unity (19142): > , 50600009
D/Unity (19142): > , 00000000
D/Unity (19142): > )
D/Unity (19142): ? getMethodID("com.github.imkira.unityurlclient.UnityURLClientBinding", "setAllowFollowRedirects", "(IZI)V", non-static)
D/Unity (19142): ! void UnityURLClientBinding.setAllowFollowRedirects(int, boolean, int);
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_FromReflectedMethod()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_DeleteLocalRef()
D/Unity (19142): > AndroidJNI_CUSTOM_CallVoidMethod(1e20057a, 57e12f88
D/Unity (19142): > , 00000004
D/Unity (19142): > , 00000001
D/Unity (19142): > , ffffffff
D/Unity (19142): > )
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_NewStringUTF()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_NewStringUTF()
D/Unity (19142): > AndroidJNI_CUSTOM_ExceptionOccurred()
D/Unity (19142): > AndroidJNI_CUSTOM_CallStaticObjectMethod(1d200522, 57ddf2a8
D/Unity (19142): > , 21800572
D/Unity (19142): > , ffa00005
D/Unity (19142): > , 50700009
D/Unity (19142): > , 00000000
D/Unity (19142): > )

"Could not initialize connection" error on Android

I have the following code working in the Unity editor, but it fails with the above message on Android.

string url = "https://www.example.com/post/here";

URLClient.HTTPRequest req = new URLClient.HTTPRequest ();
req.SetURL (url);
req.AppendQueryParameter ("id", id);
req.SetHeader ("Authorization", "Basic " + System.Convert.ToBase64String (System.Text.Encoding.UTF8.GetBytes (user + ":" + pass)));
req.Post ();

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

    if (client.Error != null) {
        Debug.Error ("Error: " + client.Error.Code + " " + client.Error.Description);
    } else {
        Debut.Log (client.Response.ContentToString ());
    }
}

I have internet permissions included in AndroidManifest.xml, and I've verified I can send requests with WWW, but I need more functionality than it offers.

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.