Search Unity

Best HTTP Released

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

  1. haswoodmagicbean

    haswoodmagicbean

    Joined:
    Mar 13, 2015
    Posts:
    5
    hi, i have problem with bestHTTP on android,but not on editor. When I try make request with editor, I got response 200. but when I compile it to apk and run it on android, I got response 400. I'm using unity 5.0.3f2 and bestHTTP version 1.9.4. everything ok when I run it with WWW class (editor and android). can you help with this? i really need to solve this fast. thanks
     
  2. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @haswoodmagicbean The plugin's code is the same for the Editor and Android.
    Status code 400 means a Bad Request, first i would setup an itermediate proxy(it's Charles for me) and check the two requests.
    In private you can send me a code snippet about how you make this failing request.
     
  3. bigticket21

    bigticket21

    Joined:
    Jan 27, 2014
    Posts:
    9
    Hi,

    I have a very strange problem with the plugin. We bought the Pro version last year around this time and used it quite a while. So far, it worked perfectly.

    Yesterday, I decided to update the plugin with the newest version, in order to try out the statistics that were added (maybe) recently. I followed the steps (delete BestHTTP folder, delete TcpImplementation dll's. Since then, BestHTTP randomly returns null responses. I have no idea what's going on... We are only using REST requests.

    I would love to use the new update as it definitely has better stuff in it, would you be able to help me resolve this problem?
     
  4. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @bigticket21 You can try out two things:
    1.) Try it out in a fresh project. I have seen multiple times that unity messed up things.
    2.) Handle the request's status. As a best practices a general callback should look like something like this:
    Code (CSharp):
    1. Uri uri = new Uri("http://httpbin.org/get");
    2.  
    3. HTTPRequest request = new HTTPRequest(uri, (req, resp) =>
    4. {
    5.     switch (req.State)
    6.     {
    7.         // The request finished without any problem.
    8.         case HTTPRequestStates.Finished:
    9.             if (resp.IsSuccess)
    10.             {
    11.                 // TODO: Handle the successful request-response case
    12.                 Debug.Log("Request Finished Successfully! Data length: " + resp.Data.Length);
    13.             }
    14.             else // Internal server error?
    15.                 Debug.LogWarning(string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
    16.                                                 resp.StatusCode,
    17.                                                 resp.Message,
    18.                                                 resp.DataAsText));
    19.             break;
    20.  
    21.         // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
    22.         case HTTPRequestStates.Error:
    23.             Debug.LogWarning("Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
    24.             break;
    25.  
    26.         // The request aborted, initiated by the user.
    27.         case HTTPRequestStates.Aborted:
    28.             Debug.LogWarning("Request Aborted!");
    29.             break;
    30.  
    31.         // Ceonnecting to the server is timed out.
    32.         case HTTPRequestStates.ConnectionTimedOut:
    33.             Debug.LogError("Connection Timed Out!");
    34.             break;
    35.  
    36.         // The request didn't finished in the given time.
    37.         case HTTPRequestStates.TimedOut:
    38.             Debug.LogError("Processing the request Timed Out!");
    39.             break;
    40.     }
    41. });
    42.  
    43. request.Send();
    Edit: i think you may try to lower the tcp-channel reuse time too. On this forum this is the most reported 'problem' of failed request. You can do this by setting the HTTPManager's MaxConnectionIdleTime to a lower value(the default is 30sec):
    Code (CSharp):
    1. BestHTTP.HTTPManager.MaxConnectionIdleTime = TimeSpan.FromSeconds(15);
     
  5. bigticket21

    bigticket21

    Joined:
    Jan 27, 2014
    Posts:
    9
    Thank you for the very quick reply. I tried reimporting the whole asset, deleting everything I could find related to it before importing again. So far it's handling well, but still returned null one time.

    As for the request's status, I already do that, so there are no exceptions, but I still need that response in order to proceed with other operations inside the game.

    The MaxConnectionIdleTime will be tested, thanks. i will report if anything strange happens. It's kind of funny though, the last version we used had
    Code (CSharp):
    1. MaxConnectionIdleTime = TimeSpan.FromMinutes(2);

    For the record, this is how I make a request. It is quite similar to your example.
    Code (CSharp):
    1. /// <summary>
    2. /// Makes a HTTP Get Request to a specified URI
    3. /// </summary>
    4. /// <param name="uri">URI to send the request to</param>
    5. /// <param name="callback">Callback function to call when the request is finished/received</param>
    6. /// <param name="fields">Optionally send some fields inside the request</param>
    7. public static HTTPRequest GetRequest(string uri, UnityEngine.Events.UnityAction<HTTPRequest, HTTPResponse> callback, Dictionary<string, string> fields = null)
    8. {
    9.     //Checks if internet is on
    10.     if (!DeviceInfo.IsConnected())
    11.     {
    12.         ErrorUI.Instance.LoadError(Constants.LocalizationKeyLabels.InternetError);
    13.     }
    14.  
    15.     //Logs a message in requests.txt
    16.     LogRequestMessage("GET", uri, fields);
    17.  
    18.     //Create uri
    19.     System.Uri systemUri = new System.Uri(uri);
    20.  
    21.     //Create the GET request
    22.     HTTPRequest newRequest = new HTTPRequest(systemUri, HTTPMethods.Get, (req, resp) =>
    23.     {
    24.         //Calls the callback if the request finished and the response is null
    25.         if (req.State == HTTPRequestStates.Finished && resp != null)
    26.         {
    27.             callback(req, resp);
    28.         }
    29.         else
    30.         {
    31.             //Logs the request error, if there is one.
    32.             if (resp == null)
    33.             {
    34.                 Debug.Log("Response is null");
    35.             }
    36.  
    37.             Debug.LogWarning("Request Finished with " + req.State.ToString() + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
    38.         }
    39.     });
    40.  
    41.     //Adds the fields
    42.     if (fields != null)
    43.     {
    44.         foreach (KeyValuePair<string, string> entry in fields)
    45.         {
    46.             newRequest.AddField(entry.Key, entry.Value);
    47.         }
    48.     }
    49.  
    50.     //Sends the request
    51.     newRequest.Send();
    52.  
    53.     //Returns the request
    54.     return newRequest;
    55. }
    56.  
     
  6. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @bigticket21
    Well, then the MaxConnectionIdleTime is out.
    What was the State of the response?

    Also, maybe the logs have something that we can use. You can turn on a more chatty logging:
    Code (CSharp):
    1. BestHTTP.HTTPManager.Logger.Level = BestHTTP.Logger.Loglevels.All;
     
  7. bigticket21

    bigticket21

    Joined:
    Jan 27, 2014
    Posts:
    9
    Well, I can't really tell you for certain as the errors stopped after I reimported the assets. Maybe that was it! (I vaguely remember that some Bouncy Castle scripts threw errors about wrong GUID or something yesterday). However, I think the request's state was ConnectionTimedOut.

    If it happens again, I'll just post the whole debug string.
     
  8. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @bigticket21 Thanks, that will be helpful.
    If it was a ConnectionTimedOut, then you can increase the ConnectTimeout property of the request:
    Code (CSharp):
    1. newRequest.ConnectTimeout = TimeSpan.FromSeconds(60);
    (Or globally through the HTTPManager class).
     
  9. JohnyK

    JohnyK

    Joined:
    Jun 13, 2014
    Posts:
    13
    Hello, does your product fully support WebSockets for WebGL Builds and iOS?
    Thanks.
     
  10. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @JohnyK Yes. On WebGL the plugin will use the underlying browser's implementation. On other builds my implementation is used that is written against the official RFC #6455.
    But you will use the same api for every platform, normally you don't have to change anything to build for WebGL.
     
    JohnyK likes this.
  11. Cheshire Cat

    Cheshire Cat

    Joined:
    Sep 18, 2012
    Posts:
    39
    Hey BestHttp,

    I've been using your plugin for a while a it does a great job, very helpful one for working with REST APIs.

    Now I got into an issue and need yous assistance. The following is the curl command want to reproduce with the BestHTTP API:

    or, as a HTTP POST request should look like:

    So I doing it by the following code, which sends the data properly, but it is not decoded well on the serve side:

    Code (CSharp):
    1. HTTPRequest request = new HTTPRequest(new Uri("https://api.api.ai/v1/query?v=20150910"), HTTPMethods.Post, onTestQuery);
    2.         request.SetHeader("Authorization", "Bearer " + DEVELOPER_ACCESS_TOKEN + "");
    3.         request.SetHeader("ocp-apim-subscription-key", SUBSCRIPTION_KEY);
    4.         request.SetHeader("Content-Type", "audio/wav");
    5.  
    6.         //Read the Audio from file
    7.         byte[] buffer = VoicesManager.Instance.loadLastRecordedWav();
    8.  
    9.  
    10.         //Attach the audio as a raw data
    11.         request.RawData = buffer;
    12.  
    13.         request.Send();
    Please assist,
    Thanks
     
  12. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Cheshire Cat

    You are almost there. With the -F switch you have to use the AddField/AddBinaryData methods:
    Code (CSharp):
    1. HTTPRequest request = new HTTPRequest(new Uri("https://api.api.ai/v1/query?v=20150910"), HTTPMethods.Post, onTestQuery);
    2. request.SetHeader("Authorization", "Bearer " + DEVELOPER_ACCESS_TOKEN + "");
    3. request.SetHeader("ocp-apim-subscription-key", SUBSCRIPTION_KEY);
    4.  
    5. //Read the Audio from file
    6. byte[] buffer = VoicesManager.Instance.loadLastRecordedWav();
    7.      
    8. string json = "{'timezone':'America/New_York', 'lang':'en'}";
    9.  
    10. request.AddBinaryData("request", System.Text.Encoding.UTF8.GetBytes(json), string.Empty, "application/json");
    11. request.AddBinaryData("voiceData", buffer, "hello.wav", "audio/wav");
    12.  
    13. request.Send()
     
  13. Cheshire Cat

    Cheshire Cat

    Joined:
    Sep 18, 2012
    Posts:
    39
    Worked like a charm, thanks :)
     
  14. Cheshire Cat

    Cheshire Cat

    Joined:
    Sep 18, 2012
    Posts:
    39
    Sorry for bothering, but here is another curl example I do not fully understand:

    How do I handle that "--data-binary" option?
     
  15. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    As i see this should be where you have to use RawData:
    Code (CSharp):
    1. Uri uri = new Uri("http://httpbin.org/post");
    2.  
    3. HTTPRequest request = new HTTPRequest(uri, HTTPMethods.Post, (req, resp) =>
    4. {
    5.     switch (req.State)
    6.     {
    7.         // The request finished without any problem.
    8.         case HTTPRequestStates.Finished:
    9.             if (resp.IsSuccess)
    10.             {
    11.                 Debug.Log("Request Finished Successfully! Data : " + resp.DataAsText);
    12.             }
    13.             else // Internal server error?
    14.                 Debug.LogWarning(string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
    15.                                                 resp.StatusCode,
    16.                                                 resp.Message,
    17.                                                 resp.DataAsText));
    18.             break;
    19.  
    20.         // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
    21.         case HTTPRequestStates.Error:
    22.             Debug.LogWarning("Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
    23.             break;
    24.  
    25.         // The request aborted, initiated by the user.
    26.         case HTTPRequestStates.Aborted:
    27.             Debug.LogWarning("Request Aborted!");
    28.             break;
    29.  
    30.         // Ceonnecting to the server is timed out.
    31.         case HTTPRequestStates.ConnectionTimedOut:
    32.             Debug.LogError("Connection Timed Out!");
    33.             break;
    34.  
    35.         // The request didn't finished in the given time.
    36.         case HTTPRequestStates.TimedOut:
    37.             Debug.LogError("Processing the request Timed Out!");
    38.             break;
    39.     }
    40. });
    41.  
    42. request.AddHeader("Authorization", "Bearer mytoken");
    43. request.AddHeader("Content-Type", "audio/wav");
    44.  
    45. request.RawData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    46.  
    47. request.Send();
     
  16. bigticket21

    bigticket21

    Joined:
    Jan 27, 2014
    Posts:
    9
    Hi,

    my null response problems just happened again.

    I captured everything that went to the console. I have activated debug logging with this:
    Code (CSharp):
    1. BestHTTP.HTTPManager.Logger.Level= BestHTTP.Logger.Loglevels.All;
    The debug log shows the following:
    Code (CSharp):
    1. 11/13/2015 1:49:05 PM: POST request to http://api........(changed the URL here)/user/login/ with fields: device: 96afefb8f6ad5f2ff766aa8ad9af20806XXXXXX
    2.  
    3. 11/13/2015 1:49:05 PM: IP: 0.0.0.0; UnityEngine.Debug:Log(Object)
    4. TowerAttack.Helpers.DeviceInfo:IsConnected() (at Assets/Scripts/Helpers/DeviceInfo.cs:15)
    5.  
    6. 11/13/2015 1:49:05 PM: Network connections: 0; UnityEngine.Debug:Log(Object)
    7. TowerAttack.Helpers.DeviceInfo:IsConnected() (at Assets/Scripts/Helpers/DeviceInfo.cs:16)
    8.  
    9. 11/13/2015 1:49:35 PM: Response is null; UnityEngine.Debug:Log(Object)
    10. TowerAttack.HTTP.<PostRequest>c__AnonStorey11A:<>m__1DE(HTTPRequest, HTTPResponse) (at Assets/Scripts/HTTP/Requester.cs:104)
    11. BestHTTP.HTTPRequest:CallCallback() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/HTTPRequest.cs:1170)
    12. BestHTTP.ConnectionBase:HandleCallback() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/Connections/ConnectionBase.cs:171)
    13. BestHTTP.HTTPManager:OnUpdate() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/HTTPManager.cs:615)
    14. BestHTTP.HTTPUpdateDelegator:Update() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/HTTPUpdateDelegator.cs:134)
    15.  
    16. 11/13/2015 1:49:35 PM: <warning>Request Finished with ConnectionTimedOut Connection timed out!
    17.   at BestHTTP.PlatformSupport.TcpClient.General.TcpClient.Connect (System.Net.IPEndPoint remoteEP) [0x00075] in C:\Users\User\Google Drive\TowerAttack SVN\Assets\Libraries\Best HTTP (Pro)\BestHTTP\PlatformSupport\TcpClient\TcpClient.cs:350
    18.   at BestHTTP.PlatformSupport.TcpClient.General.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x000b3] in C:\Users\User\Google Drive\TowerAttack SVN\Assets\Libraries\Best HTTP (Pro)\BestHTTP\PlatformSupport\TcpClient\TcpClient.cs:444 ; UnityEngine.Debug:LogWarning(Object)
    19. TowerAttack.HTTP.<PostRequest>c__AnonStorey11A:<>m__1DE(HTTPRequest, HTTPResponse) (at Assets/Scripts/HTTP/Requester.cs:107)
    20. BestHTTP.HTTPRequest:CallCallback() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/HTTPRequest.cs:1170)
    21. BestHTTP.ConnectionBase:HandleCallback() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/Connections/ConnectionBase.cs:171)
    22. BestHTTP.HTTPManager:OnUpdate() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/HTTPManager.cs:615)
    23. BestHTTP.HTTPUpdateDelegator:Update() (at Assets/Libraries/Best HTTP (Pro)/BestHTTP/HTTPUpdateDelegator.cs:134)
    24. </warning>
    Basically nothing came from BestHTTP's logging. When it works, it sends messages on each request ("
    I [HTTPConnection]: Connected to api........(url changed).net:80", etc)
    , but not this time.

    It fixed itself by restarting, but it's not a good workaround when publishing the product.
     
  17. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @bigticket21 It wasn't able to log the "Connected to ..." debug string because it wasn't able to connect to. The request's state was ConnectionTimedOut.

    You can increase the ConnectTimeout property of the reqest:
    Code (CSharp):
    1. newRequest.ConnectTimeout = TimeSpan.FromSeconds(120);
    But, as you have the Pro version you can switch back to the old behavior when no connection timeout was implemented: In the \Best HTTP (Pro)\BestHTTP\PlatformSupport\TcpClient\TcpClient.cs remove line 335 through 351, then uncomment lines 369 and 370.
     
  18. bigticket21

    bigticket21

    Joined:
    Jan 27, 2014
    Posts:
    9
    Will test both, and report results.

    Thanks for the support :)
     
  19. numabus

    numabus

    Joined:
    Nov 9, 2015
    Posts:
    4
    Hi, Best HTTP.

    I'm new to Best HTTP and C#.

    I just want to implement HTTP connection by self signed In memory certification .

    for example standard C#


    HttpWebRequest request = (HttpWebRequest) WebRequest.Create ( "URL" );
    request.Method = "POST";
    TextAsset certText = "-----BEGIN CERTIFICATE-----....certificate_stream....-----END CERTIFICATE-----";
    X509Certificate2 certFile = new X509Certificate2 ( certText.bytes );
    request.ClientCertificates.Add ( certFile );
    (and post data...)​

    I cannot find a way to implement self signed in memory certification, in BestHTTP.

    What can be best example for me?
     
    Last edited: Nov 14, 2015
  20. potterdai

    potterdai

    Joined:
    Apr 23, 2014
    Posts:
    10
  21. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @numabus Already tried to studio how client certificate sending should work, but wasn't able to find too much help about it.
    With the help of the bundled BouncyCastle implementation it should work somehow, but i can't say that it's possible and i can't help either, sorry.
     
  22. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @potterdai It's already on my TODO list, and it's climbing higher and higher. ;)
     
  23. 6565

    6565

    Joined:
    Feb 17, 2013
    Posts:
    19
    Hello,

    We're testing Best HTTP (we have the pro version) to use to connect via WebSockets to a nodejs socket.io server.

    Now I see that by default the SocketManager uses the polling transport, and I can't find any way to change it.
    So my question is, how can I enable SocketManager to use WebSockets?

    Thanks
     
  24. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @6565 If the upgrades list in the handshake data contains the websocket transport, the SocketManager will try to connect to the server using the WebSocket transport. If it fails, then it will fall back to polling.
    The first, handshake request also has the transport=polling param, but it will then try to upgrade to websocket.
     
  25. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    215
    Hi BestHTTP,


    How can do for upload a audio.wav? or send audioclip like a wav?



    Greetings.-
     
  26. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Onsterion Well, it depends on how your server expect it. You can send it by adding to the request using the AddBinaryData (it will send it like a web form), or you can send it through the RawData property.
     
  27. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    215
    This example is provided in the web api:

    Code (CSharp):
    1. POST /query? scenarios=catsearch&appid=f84e364c-ec34-4773-a783-73707bd9a585&locale=en-US&device.os=wp7&version=3.0&format=xml&requestid=1d4b6030-9099-11e0-91e4-0800200c9a66&instanceid=1d4b6030-9099-11e0-91e4-0800200c9a66 HTTP/1.1
    2. Host: speech.platform.bing.com/recognize/query
    3. Content-Type: audio/wav; samplerate=8000
    4.  
    5. (audio data)
     
  28. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Onsterion For this you have to use the RawData property:

    Code (CSharp):
    1. Uri uri = new Uri("http://speech.platform.bing.com/recognize/query?scenarios=catsearch&appid=f84e364c-ec34-4773-a783-73707bd9a585&locale=en-US&device.os=wp7&version=3.0&format=xml&requestid=1d4b6030-9099-11e0-91e4-0800200c9a66&instanceid=1d4b6030-9099-11e0-91e4-0800200c9a66");
    2.  
    3. HTTPRequest request = new HTTPRequest(uri, HTTPMethods.Post, (req, resp) =>
    4. {
    5.     switch (req.State)
    6.     {
    7.         // The request finished without any problem.
    8.         case HTTPRequestStates.Finished:
    9.             if (resp.IsSuccess)
    10.             {
    11.                 Debug.Log("Request Finished Successfully! Data : " + resp.DataAsText);
    12.             }
    13.             else // Internal server error?
    14.                 Debug.LogWarning(string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
    15.                                                 resp.StatusCode,
    16.                                                 resp.Message,
    17.                                                 resp.DataAsText));
    18.             break;
    19.  
    20.         // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
    21.         case HTTPRequestStates.Error:
    22.             Debug.LogWarning("Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
    23.             break;
    24.  
    25.         // The request aborted, initiated by the user.
    26.         case HTTPRequestStates.Aborted:
    27.             Debug.LogWarning("Request Aborted!");
    28.             break;
    29.  
    30.         // Ceonnecting to the server is timed out.
    31.         case HTTPRequestStates.ConnectionTimedOut:
    32.             Debug.LogError("Connection Timed Out!");
    33.             break;
    34.  
    35.         // The request didn't finished in the given time.
    36.         case HTTPRequestStates.TimedOut:
    37.             Debug.LogError("Processing the request Timed Out!");
    38.             break;
    39.     }
    40. });
    41.  
    42. request.AddHeader("Content-Type","audio/wav;samplerate=8000");
    43.  
    44. // TODO: Set the audio data to the RawData property
    45. request.RawData = new byte[0];
    46.  
    47. request.Send();
     
  29. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    215

    Mmmm i get:
    Request Finished Successfully, but the server sent an error. Status Code: 403-Forbidden

    I send: https://speech.platform.bing.com/re...nstanceid=b2c95ede-97eb-4c88-81e4-80f32d6aee5



    If i need a token how can i do?
     
    Last edited: Nov 18, 2015
  30. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
  31. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    215
    Well after too many tests sending tokens, now it's working like a charm, thanks again BestHTTP.
     
    Last edited: Nov 19, 2015
  32. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
    Hello Best.

    Please confirm that WebGL builds in 5.2.2 populate the HTTP response headers? Mine are null.

    I am calling using:
    public HTTPRequest(Uri uri,HTTPMethods methodType,OnRequestFinishedDelegate callback)
    Thanks
    Chris
     
  33. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Chris HG
    Will check it asap.
    However the browser will not disclose all headers it's received. It might be possible that the server sent only a few headers that the browser will not let the plugin access.
     
  34. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
    Thanks. For now all I care about is the Status.

    Here is the Mockbin setup I am using:
    Code (csharp):
    1. {   "status": 200,   "statusText": "OK",   "httpVersion": "HTTP/1.1",   "headers": [     {       "name": "Access-Control-Allow-Origin", "value": "*" }, { "name": "Access-Control-Expose-Headers", "value": "*" } ],   "cookies": [],   "content": {     "mimeType": "application/json", "text": "", "size": 0 },   "redirectURL": "",   "bodySize": 0,   "headersSize": 0 }
     
  35. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Chris HG Just run a quick test built with Unity 5.2.2f1, and I received headers. In the callback i logged them out.However as I mentioned only the "Last-Modified" and "Content-Type" headers were accessible.

    Can you elaborate more on what Status are you interested in, and what is your use-case that you experience problem with?
     
  36. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Chris HG Also, in the browser's console you can find a lot of information about the ongoing requests/responses.
     
  37. Chris-HG

    Chris-HG

    Joined:
    Aug 10, 2012
    Posts:
    63
    Wait, nevermind. I was looking for the HTTP Status Code in the headers. Thanks.
     
  38. fat_flying_pigs

    fat_flying_pigs

    Joined:
    Feb 3, 2014
    Posts:
    4
    I'm trying to do a REST call that requires me to have a user/pass in the header and a certificate. Can I use BestHTTP with the .pfx certificate file to make the REST request?

    I appears that I can use request.AddHeaders for the user/pass portion... What about the certificate? To be clear, I have the .pfx certificate as a byte[] or as a string - I do not want to use the OSX/Windows/iOS native keystores.

    Edit: For clarification, a web browser will normally do the following: url is typed (https://user:pass@example.com), the browser prompts the user to pick a certificate to validate themselves, the user picks the cert, the browser responds with the page data.
     
    Last edited: Nov 19, 2015
  39. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @fat_flying_pigs

    If you have to send the pfx bytes in a header then most probably you have to Base64 convert it first:
    Code (CSharp):
    1. request.AddHeader("X-Client-Cert", Convert.ToBase64String(pfxBytes));
    Requests in "user: passwd@example.com" form usually translated to Basic or Digest authentication, I have to say I haven't seen an example where I have had to pick a certificate, so i can't really imagine how it's done.
     
  40. Deleted User

    Deleted User

    Guest

    Hi,

    My name is Daniel and I'm working on a game and I wanna use a Laravel api as backend
    I wanna know if your asset can exchange data between android/ios and laravel.

    I'll use things like: sing in and sing up forms, user profile/account, search form, etc...

    and i'm using the 'Eloquent authentication driver'.

    is it possible ??

    best regards,
    Daniel
     
  41. fat_flying_pigs

    fat_flying_pigs

    Joined:
    Feb 3, 2014
    Posts:
    4
    Following up on my last question...
    The thing I am trying to do is the bottom-most diagram here: https://docs.oracle.com/cd/E19226-01/820-7627/bncbs/index.html

    I can send a request to the server and get a certificate back (parts 1 & 2) using an ICertificateVerifyer. Assuming I can figure out step 3, how would I go about sending step 4 in the diagram? I'm new to networking and still very confused...
     
  42. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @DannValois
    Hi Daniel.

    There are others who already using my plugin with Laravel. You can search for one in the Pro version's reviews.
    The plugin will work on android and on ios too.

    I didn't know or used Laravel myself, so I didn't know what's the 'Eloquent authentication driver', but if you can use the result from a browser, then you will be able to do the same with my plugin.

    One advice that can help speed up development is an intermediate proxy that you can use to see what format the browser sends the requests, and you can form your requests for your game to see the same. I can recommend Charles and Fiddler as proxies.

    And, as always, I'm here to help.
     
  43. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @fat_flying_pigs I think it's depend on what your server expects. You can use the less secure form-based authentication if you send it over HTTPS. But it can be a Basic or Digest Authentication too. Or an OAuth 2 Bearer token.

    I think this is why it isn't specified in that documentation, because you have more options for step 4.
     
  44. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @fat_flying_pigs

    So, how can you send your username: password pair for various authentication types:
    -Basic Authentication:
    Code (CSharp):
    1. var request = new HTTPRequest(new Uri(url), OnFinished);
    2. request.Credentials = new Credentials(AuthenticationTypes.Basic, "username", "password");
    3. request.Send();
    -Digest Authentication:
    Code (CSharp):
    1. var request = new HTTPRequest(new Uri(url), OnFinished);
    2. request.Credentials = new Credentials(AuthenticationTypes.Digest, "username", "password");
    3. request.Send();
    -Form based authentication:
    Code (CSharp):
    1. var request = new HTTPRequest(new Uri(url), HTTPMethods.Post, OnFinished);
    2. request.AddField("username", "my username");
    3. request.AddField("password", "my secure password");
    4. request.Send();
     
  45. MaxPalmer

    MaxPalmer

    Joined:
    Mar 9, 2013
    Posts:
    27
    Hi,

    I'm pretty new to authentication, but I have a requirement to pull data from a server that requires Windows/Kerberos authentication in Unity, targeting iOS. Does BestHttpPro handle this or do you know of a solution that supports this on Unity?
     
  46. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @MaxPalmer Unfortunately Windows/Kerberos authentication isn't supported yet. Also, didn't know about any other plugin that supports it, sorry.
     
  47. MaxPalmer

    MaxPalmer

    Joined:
    Mar 9, 2013
    Posts:
    27
    That's a shame. Thanks for replying.
     
  48. jtrice

    jtrice

    Joined:
    Aug 29, 2012
    Posts:
    7
    Speaking of authentication -- works fine in PC/iOS
    WebGL is failing. Chrome/FF Dev tools indicate the Credential Headers are empty.
    Am I misusing the below?
    Do I need the AuthenticationTypes as exemplified above?

    Code (CSharp):
    1.         HTTPRequest request = new HTTPRequest(new Uri(companyConfigReqAuth));
    2.         request.Credentials = new Credentials(UnameAuth, PwordAuth);
    3.         request.Send();
    4.  
    (BTW - great GREAT library - much thanks)
     
  49. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @jtrice Thanks for the compliment!
    If you set the Credentials property then the plugin will pass these to the browser in a http://user:passwd@httpbin.org/basic-auth/user/passwd form. With this type of authentication an Authorization header will be set.
    Did a quick test, and it's worked on WebGL:
    Code (CSharp):
    1. Uri uri = new Uri("http://httpbin.org/basic-auth/user/passwd");
    2.  
    3. HTTPRequest request = new HTTPRequest(uri, (req, resp) =>
    4. {
    5.     switch (req.State)
    6.     {
    7.         // The request finished without any problem.
    8.         case HTTPRequestStates.Finished:
    9.             if (resp.IsSuccess)
    10.             {
    11.                 Debug.Log("Request Finished Successfully! Data : " + resp.DataAsText);
    12.             }
    13.             else // Internal server error?
    14.                 Debug.LogWarning(string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
    15.                                                 resp.StatusCode,
    16.                                                 resp.Message,
    17.                                                 resp.DataAsText));
    18.             break;
    19.  
    20.         // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
    21.         case HTTPRequestStates.Error:
    22.             Debug.LogWarning("Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
    23.             break;
    24.  
    25.         // The request aborted, initiated by the user.
    26.         case HTTPRequestStates.Aborted:
    27.             Debug.LogWarning("Request Aborted!");
    28.             break;
    29.  
    30.         // Ceonnecting to the server is timed out.
    31.         case HTTPRequestStates.ConnectionTimedOut:
    32.             Debug.LogError("Connection Timed Out!");
    33.             break;
    34.  
    35.         // The request didn't finished in the given time.
    36.         case HTTPRequestStates.TimedOut:
    37.             Debug.LogError("Processing the request Timed Out!");
    38.             break;
    39.     }
    40. });
    41.  
    42. request.Credentials = new Credentials(AuthenticationTypes.Basic, "user", "passwd");
    43. request.Send();
    You can also check your browser's console for additional logs.
     
  50. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @jtrice Sent to you a message with a new WebRequest.jslib that you can try out. Maybe it will help.