Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Best HTTP Released

Discussion in 'Assets and Asset Store' started by BestHTTP, Sep 11, 2013.

  1. RobDaPraia

    RobDaPraia

    Joined:
    May 18, 2014
    Posts:
    9
    Is it possible with your tool to:

    1- read a GZip file
    2- create a GZip file


    The reason is that i'm trying to save scene data into a compressed json file and later read this compressed json file back into the scene. However have not found a solution with other libraries like SharpZipLib (compiles in editor but not on phone) to do this, interested to know if this would be possible with your asset?
     
  2. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    With the Pro version you have access to the internal GZipStream class, that you can use to compress/decompress a stream.

    Compressing:
    Code (CSharp):
    1. using BestHTTP.Decompression.Zlib;
    2.  
    3. using (FileStream fs = new FileStream("path to file", FileMode.Create))
    4.     using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
    5.     {
    6.         //gs.Write
    7.     }

    Decompressing:
    Code (CSharp):
    1. using BestHTTP.Decompression.Zlib;
    2.  
    3. using (FileStream fs = new FileStream("path to file", FileMode.Open))
    4.     using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
    5.     {
    6.         //gs.Read();
    7.     }
    Or you can use GZipStream's static CompressBuffer/CompressString and UncompressBuffer/UncompressString functions.
     
  3. RobDaPraia

    RobDaPraia

    Joined:
    May 18, 2014
    Posts:
    9

    Thanks!

    I just bought the asset and got it working in Unity and build for the Windows Phone. I only included the Decompression folder of the asset. The code works in the editor game and on the actual phone as well.

    However, if I want to build for Windows Store Apps, I got the following errors. Hope you can help me because I bought the asset for working with GZip files on WIndows Phone and Windows Store Apps.

    Code (CSharp):
    1. Assets\Best HTTP (Pro)\BestHTTP\Decompression\CRC32.cs(805,30): error CS0115:
    2. 'BestHTTP.Decompression.Crc.CrcCalculatorStream.Close()':
    3. no suitable method found to override
    4.  
    5. Assets\Best HTTP (Pro)\BestHTTP\Decompression\ZlibBaseStream.cs(302,30): error CS0115:
    6. 'BestHTTP.Decompression.Zlib.ZlibBaseStream.Close()':
    7. no suitable method found to override
    8.  
    9. Error building Player because scripts had compiler errors
    10.  


    I used the following code in Unity free version:

    Code (CSharp):
    1. public static string ExtractGZip(string filePath)
    2.     {
    3.         var result = string.Empty;
    4.  
    5.         // example in a resources folder: filePath="LevelData/Level_000.json.gz"
    6.         // note: add ".bytes" to file in actual folder: "Assets/_MyGame/LevelData/Level_000.json.gz.bytes"
    7.         // to tell Unity it is a binary file
    8.  
    9.         var levelData = Resources.Load<TextAsset>(filePath);
    10.         if (levelData != null)
    11.         {
    12.             // using (var fs = new FileStream(filePath, FileMode.Open))
    13.             using (var fs = new MemoryStream(levelData.bytes))
    14.             using (var gs = new GZipStream(fs, CompressionMode.Decompress))
    15.             using (var ms = new MemoryStream())
    16.             {
    17.                 var dataBuffer = new byte[2048];
    18.                 var size = dataBuffer.Length;
    19.  
    20.                 while (true)
    21.                 {
    22.                     size = gs.Read(dataBuffer, 0, size);
    23.                     if (size > 0)
    24.                     {
    25.                         ms.Write(dataBuffer, 0, size);
    26.                     }
    27.                     else
    28.                     {
    29.                         break;
    30.                     }
    31.                 }
    32.                 var byteArray = ms.ToArray();
    33.                 result = GetString(byteArray);
    34.             }
    35.         }
    36.         if (result == null)
    37.         {
    38.             Debug.LogError("CompressionHelper, ExtractGZip(), ERROR==>(result == null)");
    39.         }
    40.         return result;
    41.     }
     
  4. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Sorry, Windows Store Apps are not supported.

    But you can try to replace the stream's Close function with Dispose (_innerStream.Close(),_baseStream.Close(), _stream.Close()), however other WSA specific compile errors may occur.
     
  5. BoinMaster

    BoinMaster

    Joined:
    Jul 22, 2014
    Posts:
    3
    Hi BestHTTP, I bought your asset, and tried WebSocket communication with it. And I found out that my app doesn't receive expected packet size.

    Then after some research on it, it seems the cause is that stream.Read(rawLen, 0, 2) that is about at 70th line on WebSocketFrameReader.cs proceeds processes as if it reads 2 bytes even though it somestimes reads only 1 byte from the stream.Read(rawLen, 0, 2).

    I tried fixing it by this below.
    rawLen[0] = (byte)stream.ReadByte();
    rawLen[1] = (byte)stream.ReadByte();

    And the problem has been resolved. Do you think this fixing has no problem?
     
    Last edited: Jul 31, 2014
  6. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Hi BoinMaster.

    Thank you for your detailed report!

    After a quick Stream.Read documentation check, I think i should swich the Read calls to ReadBytes, or i should check it's return values, as there might be cases(as you experienced :)) where Read returns less bytes.

    Double checked, and I didn't made this mistake in other cases.

    So, answering your question, I think it's a better solution then my current one, and you should use it. :)
     
  7. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    New version pending in the store!

    Changelog of this relese:
    [BugFix] In some cases a WebSocket message read incorrectly
    [New Feature] HTTPRequest can be used with yield return (see the docs for more information)
    [New Feature] Install script to circumvent manual folder moving
    [Improvement] Greatly improved link.xml​
     
  8. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Sorry if this was posted before, but does the Basic version work with Unity WebPlayer?
     
  9. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Only the Pro version supports the WebPlayer build, as it have to disable certain features that uses file saving(like cache, cookies).
     
  10. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Is there any way to upgrade to the pro version? We have the basic right now and use it quite extensively. We plan to make a Facebook Canvas page.
     
  11. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    The only way i know is that we have to request a refund on your Basic purchase(send me your invoice number) while you have to buy the Pro version...
    As assets can't be linked, the Basic and Pro versions are two distinct 'thing' in the store.
     
  12. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Alright, the other I question I have is how easy is the upgrade process? Are all the calls and stuff essentially the same? Can I just import the pro version over, or do I need to delete the Basic stuff first?
     
  13. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    The two plugins are the same API wise, you don't have to change a line in your code.

    To upgrade, first you have to delete the 'Best HTTP (Basic)' folder, and all corresponding dlls in the /Plugin/ folder. After this you can import the Pro package.

    P.S.: For WebPlayer builds, please make sure you checked the docs for restrictions for the plugin(especially for the Socket Policy Service).
     
  14. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Thanks for the info on the upgrade process.

    I'm still a little iffy on the whole Socket Policy Service stuff. I'll start testing it once I upgrade to the pro version.
     
  15. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Fortunately Unity comes bundled with a Socket Policy Service, you can find it in the %Unity Install Dir%/Editor/Data/Tools/SocketPolicyServer/. The precompiled sockpol.exe can be run on linux too with mono.
     
  16. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    No matter what we try right now, we keep getting "Unable to connect, as no valid crossdomain policy was found"

    We have a crossdomain.xml file at the root. We had our sockpol.exe and we could telnet test it. We had the sockpol generatings its own crossdomain policy that we could call. With and without the Security.PrefetchSocketPolicy(). And through the editor with the emulation settings on and even when placing the unity3d and html files on the server.

     
  17. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Using a Socket no crossdomain.xml file are involved in the connection process. So you shouldn't specify it with the --file argument.
    Common mistake that a --local argument has a port range of 4500-4550, so the plugin can't access to port 80 for http, or port 443 for https.

    If you still have problem with it, please email me your server address and i will try to find out what's the problem.
     
  18. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Well, I came into the office today, and it started working...Not exactly sure why since I don't think we've changed anything since yesterday haha. Thanks again for all the help! Great support :D
     
  19. michaelmei81

    michaelmei81

    Joined:
    Aug 9, 2014
    Posts:
    11
    Hi there,

    We have implemented a HTTP restful server.
    We tried to use RestCharp lib on github as the client integrated to unity3d. It works perfectly on android but not on IOS platform. Could you please let me know if this plugin provide sufficient interfaces for restful services? It works on both platforms?
     
  20. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    You can create Get, Post, Head, Put, Delete and Patch requests. And all features that are listed in the documentation are fully supported on iOS, Android and Windows Phone 8 (plus ofc on desktop builds and editor too).
    The WebPlayer is a little restricted, but it's still fully workable without any code modification on client side.
     
  21. jlegrice

    jlegrice

    Joined:
    Nov 21, 2012
    Posts:
    6
    Hello!

    First of all, great plugin. Really simple, really nice to use, and really nice source to pour through.

    I've had an idea recently that involves making some web requests from editor scripts, but to my dismay BestHTTP falls over JUST before my callback would have been invoked. Looks like it's because you rely on LateUpdate's to recycle your connection objects?

    It would be great if you could rework it a bit. For now, I've got around it by spinning my call to Send() off on another thread, and calling HTTPManager.OnUpdate(); from there (amongst other smallish tweaks!).

    Any chance of editor-time support becoming a feature? Would be really useful! :)
     
  22. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Hi jlegrice.

    I had an email conversation with this problem. Back then I tried to add an [ExecuteInEditMode] attribute to the HTTPUpdateDelegator class, but it doesn't worked for me, so made public the HTTPManager's OnUpdate function an called it in the EditorWindow's OnGUI event. This worked fine.
    However for my mail partner the [ExecuteInEditMode] attribute worked just fine...
    Becouse these strange outcome I left it out from the updates.

    Ofc, these are working solutions only with pro.

    Can you try it out with the added [ExecuteInEditMode] attribute? I will check it too one more time, and if it's working for you too, i will include it in the next update.
     
  23. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    New version uploaded to the store.

    Changelog of this new release:
    [BugFix] Form sending doesn’t handled correctly in some cases
    [Improvement] Rewrote form sending. Now correctly supports Url-Encoded and Multipart forms
    [New Feature] Download aborting. An ongoing download can be aborted now through a HTTPRequest object’s Abort() function
    [New Feature] New HTTPRequestStates enum and State property in the HTTPRequest class to be able to check the current state of the request​
     
  24. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Another update uploaded.

    Changelog of this update:
    • [Bugfix] Fixed a bug that intruduced in 1.4.0 that prevented a WebSocket connection to connect to the host in a WebPlayer build
    • [Improvement] Host header can be set now without overridden by the plugin
    • [Improvement] Improved thread safety. Now sending requests on multiple threads are completly safe too.
     
  25. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Last update is live now.
     
  26. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    372
    The following (namely posting a field who's value is an empty string) causes two back-to-back requests to hit our server a couple ms apart and results in request.Exception and result both being null in the OnRequestFinished handler. Bug?

    HTTPRequest request = new HTTPRequest(newUri("http://www.google.com"), HTTPMethods.Post, OnRequestFinished);
    request.AddField("EMPTY", "");
    request.Send();
     
  27. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Hi slumtrimpet.

    It's a bug on my side. I never thought that someone want to send an empty string, but if you wish... :)
    So, I can send you a patched file if you have the Pro version. If you have the Basic, then you have to wait a bit while i create a new package(next day).
    (Not becouse of the basic version itself have lower priority, but it getting very late here and i dont want to send you a buggy version.)
    Anyhow, drop a mail to besthttp@gmail.com.
     
  28. thready

    thready

    Joined:
    Sep 23, 2014
    Posts:
    7
    I have the free version of Unity. I just bought "good ol sockets" as you suggested. Got the following code below working from unity in Windows (I have an http server listening and displaying the word "WORKS"), but when I deploy to Android, it doesn't work. Please help if you can.

    HTTPRequest request = new HTTPRequest(new Uri("http://10.10.50.1:8000?info=WORKS"), OnRequestFinished);
    request.Send();

    Also, the same url from chrome on the same device works...

    Thanks,
    Mike
     
  29. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Hi thready.

    As i see you already wrote in the Good 'ol Sockets support thread, there you most likely will get the answer for your questions about this problem.
     
  30. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    372
    Thanks for the quick reply! I can wait on an update to the Basic version. I put in a work around on both the client and server side for now so I'm rolling forward without my SUPER important ;) blank post values. Will you roll this patch into the main asset distro so I can just pull it form the store at sometime in the next month or so perhaps?
     
  31. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Yes, it will be in the next update(hopefully out in the next week).
     
  32. Konjo12

    Konjo12

    Joined:
    Feb 7, 2013
    Posts:
    5
    Hello BestHTTP,

    Is there any news on supporting Windows RT? As it grows in popularity, it would be great to have a support there.
    Btw. Great work so far!
     
  33. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Hi Konjo12.

    Well, there are some news. I was able to run my demos on WinRT last weekend. (Exept the rest api example as the json lib i choose initially need some additional work, or i have to choose another one...)
    It's a very early version, but now at least I see light in the end of the tunnel now. I'm committed to the WinRT platform, but with less popularity I wrote some additional features instead of spending time on it.

    But it's still in an early stage, last time i stuck with native https support(AlternateSSL is working fine though).
     
  34. KristofVerbiest

    KristofVerbiest

    Joined:
    Apr 29, 2014
    Posts:
    2
    Is it possible to set a time-out on a request? If the server takes a long time to respond (or never responds at all) my app hangs. It would be great if I could set a time-out on a request, and after this time the callback is invoked (with an error of course).
     
  35. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Thank you for your feedback.
    You are right, the plugin is lacking the timeout settings, this is why i working on it currently. I can't say when will it be done, but i hope that in the coming weeks will be stable enough to release.
    (I have to rewrite some parts of the socket handling code.)
     
  36. DeltaCygniLabs

    DeltaCygniLabs

    Joined:
    Nov 27, 2013
    Posts:
    14
    Hi BestHTTP!
    First congrats for your work, it's by far the best purchase I've ever done on the Asset Store!
    I have a question for you: how the check the validity of an SSL certificate? I have the Pro version.
     
  37. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Thanks for the compliment.

    Well, in the HTTPConnection.cs's Connect function there is a line:
    Code (CSharp):
    1. SslStream sslStream = new SslStream(Client.GetStream(), false, (sender, cert, chain, errors) => true);
    Here you can edit the lambda function, so you can place your validation code.
    It will work on non Windows Phone 8 platforms(iOS, Android, desktop).

    In my dev. branch i have a version where you can set a function per request to validate the certificate called from this event, but i feel it's a little ugly as i can't support it on every platform that i support.
     
    DeltaCygniLabs likes this.
  38. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    It was an answer to where you can validate.
    To the 'how' question, i don't want to answer, as i don't really know how you should and don't want to give you a securely bad tip. :)
     
  39. DeltaCygniLabs

    DeltaCygniLabs

    Joined:
    Nov 27, 2013
    Posts:
    14
    Thank you for the swift reply!
    I have some ideas about the "how", will do it by accessing the Stream member values of your class.
    Do not hesitate to share any implementation that you have :)
     
  40. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    I will release it then in the next update, and I will see what's are the feedbacks.
     
  41. Robota

    Robota

    Joined:
    Feb 7, 2012
    Posts:
    82
    Hi, I'm trying to use HTTP Pro with the Web player to make HTTP request.
    I have put a crossdomain.xml file on the server like this one:

    Code (xml):
    1. <?xml version="1.0"?>
    2. <cross-domain-policy>
    3. <allow-access-from domain="*"/>
    4. </cross-domain-policy>
    But when I try to make a request I got an error: "Unable to connect, as no valid crossdomain policy was found:"

    In the documentation it is said that we must "run Socket Policy Service on the server".
    Does that means that HTTP Pro use socket to make HTTP request on the Web Player?
    Is it possible to not use sockets in the web player to make HTTP request?

    EDIT: I got my response by mail ;)
     
    Last edited: Oct 15, 2014
  42. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Hi Robota.

    Just answered your mail too, please check your mail box.
     
  43. Robota

    Robota

    Joined:
    Feb 7, 2012
    Posts:
    82
    Does the editor fully emulate all the process (checking for a correct crossdomain.xml) when we are in web player target ?
     
  44. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    I'm on the road now so i can't check it, but i belive that it's emuleting it well.
     
  45. Robota

    Robota

    Joined:
    Feb 7, 2012
    Posts:
    82
    So in the WebPlayer, it is impossible to get http request without using web socket?
    Even if I only use WWW class?
     
  46. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    There are two modes to send a HTTP request:
    -Using the WWW class. In this case the server have to have the crossdomain.xml.
    -Using a plugin(mine, or other's) that uses mono's Socket class. In this case the server must run a Socket Policy Service.
    These are limitations by Unity.

    A WebSocket is a term that describes a higher level protocol that builds on top of http. For a regular http request you don't have to use WebSocket.

    Also, if you find out that you can't use my plugin as you thought, send me your invoice number and will refund your purchase.
     
  47. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
  48. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Tried the url that you wrote in that pos, and the response was a valid json object.
    My request was:
    Code (csharp):
    1.  
    2. var request = new HTTPRequest(new Uri("https://www.googleapis.com/games/v1/players/me/leaderboards/CgkI9t63yvISEAIQAA/scores/ALL_TIME"), (req, resp) =>
    3. {
    4.    Debug.Log(resp.DataAsText);
    5. });
    6.  
    7. request.Send();
    8.  
    And the response:
    Code (csharp):
    1.  
    2. {
    3. "error": {
    4.   "errors": [
    5.   {
    6.   "domain": "global",
    7.   "reason": "required",
    8.   "message": "Login Required",
    9.   "locationType": "header",
    10.   "location": "Authorization"
    11.   }
    12.   ],
    13.   "code": 401,
    14.   "message": "Login Required"
    15. }
    16. }
    17.  
     
  49. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    Its valid json when you open the link on browser but not on android device or unity editor.
     
  50. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,665
    Received the same response on android and in the editor too.