Forum Xamarin.Forms

How to download file from secure server using dependency service

I created a dependency service "downloadFileManager".
In Droid i implemented this function :

public void DownloadFile(string uri, string filename)
    var cookies = GetCookies();
  Android.Net.Uri contentUri = Android.Net.Uri.Parse(uri);

  var deviceIdentifier = new DeviceHelper();

  Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(contentUri);

  r.AddRequestHeader("Accept", "*/*");
  r.AddRequestHeader("Accept-Encoding", "gzip, deflate, sdch, br");
  r.AddRequestHeader("Uuid", deviceIdentifier.GetIdentifier());

  foreach (var cookie in cookies)
    r.AddRequestHeader("Cookie", cookie);

  r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);



  Android.App.DownloadManager dm = (Android.App.DownloadManager)Forms.Context.GetSystemService(Context.DownloadService);


When i try to download a file from demo site like "" the download works fine but when i try to download from my (secured) server the download "unsucessful" and in the iis log i see that the request get "401" error.

I checked the request and got the session cookie.

What did i miss??

Thank you.


Sign In or Register to comment.