Search Unity

using HttpWebRequest for Partial Download

Discussion in 'Scripting' started by HYPERSONICs, Nov 30, 2017.

  1. HYPERSONICs

    HYPERSONICs

    Joined:
    Dec 10, 2014
    Posts:
    5
    Hi:

    I am trying to implement partial download in my Unity project.
    So far as my knowledge, UnityWebRequest does not have the AddRange() as System.Net.HttpWebRequest.
    It seems System.Net one is my only choose here.

    However, in my implementation, (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse() having a performance issue that drop fps lower than 20 even inside StartCoroutine with some yield return null.

    Is there any suggestion on that issue? thanks.

    Code (CSharp):
    1. System.Net.HttpWebRequest myHttpWebRequest = WebRequest.Create(url);
    2. myHttpWebRequest.AddRange(i, i + windowSize - 1);
    3. yield return null;
    4.     using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
    5.     {
    6.         yield return null;
    7.         //Do sth for these bytes
    8.      }
     
    amir0033 likes this.
  2. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    You can do partial downloads using UnityWebRequest.

    Code (csharp):
    1.  
    2. var download = new UnityWebRequest(_url, UnityWebRequest.kHttpVerbGET, downloadHandler, null);
    3. var rangeHeader = "bytes=" + resumeOffset + "-";
    4. download.SetRequestHeader("Range", rangeHeader);
    5.  
     
    amir0033 likes this.
  3. HYPERSONICs

    HYPERSONICs

    Joined:
    Dec 10, 2014
    Posts:
    5
    Thanks, I just rewrite the HttpWebRequest part in UnityWebRequest

    I also encounter another problem that when the UnityWebRequest redirectLimit not set to 0, it gets 404; set redirectLimit to 0, it says 301 Moved Permanently. have no clue how to deal with it.
     
  4. AgnosiaGames

    AgnosiaGames

    Joined:
    May 26, 2020
    Posts:
    57
    Code (CSharp):
    1. var download = UnityWebRequest.Post(url, form);
    2. download.SetRequestHeader("Range", "bytes=0-100");
    3. download.SendWebRequest();
    This gives full of data. but range is "bytes=0-100". What is problem in these codes?
     
  5. RomainF-Ubisoft

    RomainF-Ubisoft

    Joined:
    Aug 16, 2019
    Posts:
    10
    Same as @AgnosiaGames it downloads the whole file although the Range header seems good.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    This is a handy way to reason about network transaction issues:

    Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

    https://forum.unity.com/threads/using-unity-for-subscription-lists.1070663/#post-6910289

    https://forum.unity.com/threads/unity-web-request-acting-stupid.1096396/#post-7060150

    And setting up a proxy can be very helpful too, in order to compare traffic:

    https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

    Otherwise you're really just guessing where the issue is happening.
     
  7. RomainF-Ubisoft

    RomainF-Ubisoft

    Joined:
    Aug 16, 2019
    Posts:
    10
    I actually understood why I had this behavior: I was asking for a file on a local hard drive (e.g. C:/) and "Range" is not supported for local files.