Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Best HTTP Released

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

  1. LexaMV

    LexaMV

    Joined:
    Feb 20, 2018
    Posts:
    28
    thanks this is a working solution!

    But what is the best way to solve this problem? If you need to send 10 requests in a row, is it possible to create a queue in BestHTTP? I didn't find anything in the documentation
     
  2. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    @LexaMV

    The plugin limits only the number of connections per server, so if you send out 10 requests in a row to the same host a maximum of 6 HTTP/1 connections will be created.
    If the plugin can use an HTTP/2 connection then only one connection will be created and the negotiated parallellism will be used with that one connection.
     
  3. LexaMV

    LexaMV

    Joined:
    Feb 20, 2018
    Posts:
    28
    Hello. Is it possible to see the full request body in the logs? Couldn't find it in the documentation.
     
  4. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    @LexaMV

    No, the body isn't. If you want to debug your requests locally, i would recommend to set up a proxy, and route the requests through it.
    You can set proxy globally for all requests:
    Code (CSharp):
    1. HTTPManager.Proxy = new HTTPProxy(new Uri(proxyURL), null, true);
    Or per-request, setting the request's Proxy property.
     
  5. unity_ACE8B55FE846915D12E9

    unity_ACE8B55FE846915D12E9

    Joined:
    Oct 14, 2021
    Posts:
    2
    Hello, there's a problem. When sending a request to playfab an exception "Server closing the connection!" occurred. All the following requests ended with the same message. Only restarting the application helped.

    What caused this problem? How to solve it?
     
  6. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    Could you follow the logging steps here under Reporting Bugs and send over the log entries?
     
  7. unity_ACE8B55FE846915D12E9

    unity_ACE8B55FE846915D12E9

    Joined:
    Oct 14, 2021
    Posts:
    2
    Oh, there's no 100% reproduction yet. We'll try to find one.
    For now, we're wondering what the situation might be "Server closing the connection"
    In this code.

    upload_2023-10-26_13-2-10.png
     
  8. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    @unity_ACE8B55FE846915D12E9

    The server is sending a HTTP/2 Go-away frame while there's still requests in-flight. The reason might in the logs.
     
  9. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,982
    @BestHTTP
    How do i increase timeout when using SSE?
    Thanks
    {"tid":5365,"div":"HTTP2ContentConsumer","msg":"Sender thread","ex": [{"msg": "Ping ACK isn't received in time!", "stack": " at
     
  10. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    You could do something like this:
    Code (CSharp):
    1. var settings = HTTPManager.PerHostSettings.AddDefault("server.domain.io");
    2. settings.HTTP2ConnectionSettings.Timeout = TimeSpan.FromSeconds(10);
     
    jGate99 likes this.
  11. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    SSE is just like any regular request, receiving payload irregularly. PINGS are handled by the underlying HTTP/2 layer in this case.
     
    jGate99 likes this.
  12. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,982
    Hi there,
    If i use this method
    string result = await request.GetAsStringAsync();;
    and there is an error, how do i get error message as string
     
  13. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    When there's an error *Async functions will throw an error: https://bestdocshub.pages.dev/HTTP/getting-started/#async-await

    Code (CSharp):
    1. private async void Start()
    2. {
    3.     // 1. Create request
    4.     var request = HTTPRequest.CreatePost("https://httpbin.org/post");
    5.  
    6.     // 2. Setup
    7.     request.UploadSettings.UploadStream = new MultipartFormDataStream()
    8.         .AddField("Field Name 1", "Field value 1")
    9.         .AddField("Field Name 2", "Field value 2");
    10.  
    11.     try
    12.     {
    13.         // 3. Send & wait for completion
    14.         var response = await request.GetHTTPResponseAsync();
    15.         // 5. Process response
    16.         if (response.IsSuccess)
    17.             Debug.Log("Upload finished succesfully!");
    18.         else
    19.             Debug.LogError($"Server sent an error: {response.StatusCode}-{response.Message}");
    20.     }
    21.     catch (AsyncHTTPException e)
    22.     {
    23.         // 6. Error handling
    24.         Debug.LogError($"Request finished with error! Error: {e.Message}");
    25.     }
    26. }
    If there's a CancellationToken, then it can throw a TaskCanceledException too.
     
    jGate99 likes this.
  14. vuthang

    vuthang

    Joined:
    Mar 7, 2017
    Posts:
    55
    Hi @BestHTTP , how I reconnect to old session in websocket?
     
  15. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    The websocket standard doesn't specify one, hence if there's one that's always application/framework specifc built on top of it.
     
    vuthang likes this.
  16. vuthang

    vuthang

    Joined:
    Mar 7, 2017
    Posts:
    55
    Does websocket support reconnect?
     
  17. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    Sorry, wrote everything but failed to answer the question itself. In short, no, it doesn't.
     
    vuthang likes this.
  18. DarkWiz_89

    DarkWiz_89

    Joined:
    Sep 13, 2023
    Posts:
    5
    @BestHTTP - Is it possible to add "Authorization" Header for each web-socket request for WebGL platform. As far as I've seen, all the internal classes setting header are excluding WebGL platform and
    Code (CSharp):
    1. HTTPRequestCustomizationCallback
    will also be called for Non WebGl platforms
     
  19. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
  20. develop_91Act

    develop_91Act

    Joined:
    Jun 19, 2019
    Posts:
    3
    Hi, I brought the Best Websocket Bundle on Unity Asset Store, it says there are 3 packages within it. But I can't get Best TLS Security for free, it charges me for 10$ anyway.
     
  21. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    Could you send me the invoice number of the purchase in private?
     
  22. develop_91Act

    develop_91Act

    Joined:
    Jun 19, 2019
    Posts:
    3
    I send your the invoice number by conversion, I tried to contact with the unity service, but no response.
     
  23. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,982
    Hi @BestHTTP
    I couldnt find "request.rawData" propery in new version, please advise
     
  24. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    In the HTTPRequest upgrade guide i wrote about it in detail: https://bestdocshub.pages.dev/HTTP/upgrade-guide/#2-httprequest-changes

    If you used RawData in v2, you can wrap the data in a MemoryStream:

    Code (CSharp):
    1. var request = HTTPRequest.CreatePost("https://...");
    2. request.UploadSettings.UploadStream = new MemoryStream(new byte[128]);
    3. request.Send();
     
    jGate99 likes this.
  25. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,982
    Hi @BestHTTP
    Could you please provide a code example that gives me a json file by using Streaming?. Documentation doesnt state how to get final full json from streaming or maybe im missing something being a noob. Please advise
    request.DownloadSettings.OnDownloadStarted += OnDownloadStarted;
     
  26. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    The documentation doesn't have a direct example as all cases are different.

    How large are your jsons?
    I think the most easier one would be to save the content into a file and use that file to parse the json.

    Code (CSharp):
    1. var temporaryCachePath = Application.temporaryCachePath;
    2. var request = HTTPRequest.CreateGet("https://...", OnRequestFinished);
    3. request.DownloadSettings.OnDownloadStarted += OnDownloadStarted;
    4. request.DownloadSettings.DownloadStreamFactory = (req, resp, handler)
    5.     => new BlockingDownloadContentStream(resp, req.DownloadSettings.ContentStreamMaxBuffered, handler);
    6.  
    7. request.Send();
    8.  
    9. private void OnDownloadStarted(HTTPRequest req, HTTPResponse resp, DownloadContentStream stream)
    10. {
    11.     Task.Run(() => SaveAndLoadJsonStream(req, stream as BlockingDownloadContentStream));
    12. }
    13.  
    14. void SaveAndLoadJsonStream(HTTPRequest req, BlockingDownloadContentStream downloadStream)
    15. {
    16.     var tempPath = Path.Combine(temporaryCachePath, req.Context.Hash.ToString());
    17.  
    18.     try
    19.     {
    20.         // Step 1: save json to file
    21.         try
    22.         {
    23.             using var jsonFile = new FileStream(tempPath, FileMode.Create);
    24.             while (!downloadStream.IsCompleted)
    25.             {
    26.                 // Take out a segment from the downloaded
    27.                 if (downloadStream.TryTake(out var buffer))
    28.                 {
    29.                     try
    30.                     {
    31.                         jsonFile.Write(buffer.Data, buffer.Offset, buffer.Count);
    32.                     }
    33.                     finally
    34.                     {
    35.                         BufferPool.Release(buffer);
    36.                     }
    37.                 }
    38.             }
    39.         }
    40.         finally
    41.         {
    42.             downloadStream.Dispose();
    43.         }
    44.  
    45.         // Step 2: parse json from file
    46.         using (StreamReader file = File.OpenText(tempPath))
    47.         {
    48.             JsonSerializer serializer = new JsonSerializer();
    49.  
    50.             var jsonObject = serializer.Deserialize(file, typeof(object));
    51.         }
    52.     }
    53.     finally
    54.     {
    55.         // Step 3: delete temp file
    56.         File.Delete(tempPath);
    57.     }
    58. }
    59.  
    60. private void OnRequestFinished(HTTPRequest req, HTTPResponse resp)
    61. {
    62.     // ...
    63. }
     
    Last edited: Apr 24, 2024
    jGate99 likes this.
  27. Grazer2

    Grazer2

    Joined:
    Jun 20, 2011
    Posts:
    87
    Hello. I'm trying to send a post request with 2 arguments (e.g. string id, int version). I would like to have a asp.net core controller that has a method that takes these 2 arguments like MyPostMethod(string id, int version). I managed to get this done with only string arguments using Form Parameters and the [FromForm] attributes, but how would I go with non string parameters here, since I cannot add them to the UploadStream it seems. Thx in advance & cheers
     
  28. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    You can use the .ToString() method (like .AddField("version", version.ToString())).
     
    Grazer2 likes this.
  29. noam_baba

    noam_baba

    Joined:
    Mar 14, 2023
    Posts:
    9
    HI, for some reason the http is super slow, I try to check the time a request take and from the moment it's enter to the asset to the moment i parse the response it's take something like 1.5 second, and with postman it's take much more faster.
     
  30. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,666
    The request collects timing information during processing it, you can print it something like this:
    Code (CSharp):
    1. void OnRequestFinished(HTTPRequest req, HTTPResponse resp)
    2.     {
    3.         // ...
    4.  
    5.         Debug.Log(req.Timing);
    6.     }
    If you share what it's printing, i might know more what's going on.
     
  31. musteafgrand

    musteafgrand

    Joined:
    Mar 13, 2024
    Posts:
    1
    Hi, is there a way to force HTTPS requests to only make calls using SSL connections? I'm not sure if BestHTTP automatically implements this mechanism: attempting to connect with HTTPS if it can't use HTTP. Currently, we consistently receive rejections from the Google Play Store, even though we use HTTPS for all requests. Google Play Store indicates that we are using HTTP. Can you clarify?"