I'm trying to use FFImageLoading in my Xamarin.iOS and Xamarin.Android apps to load images from url. All the url's require authentication..
Also I'm using modernhttpclient
for all other Rest Calls. For modernhttpclient
this is how I'm adding authentication
var cookieHandler = new NativeCookieHandler ();
var messageHandler = new NativeMessageHandler (false, false, cookieHandler);
cookieHandler.SetCookies (RestApiPaths.cookies);
using (var client = new HttpClient (messageHandler)) {
client.DefaultRequestHeaders.TryAddWithoutValidation ("User-Agent", GetUserAgent (platform));
var r = await client.GetAsync (new Uri (url));
and it is working very well. I did same thing for FFImageLoading,
var cookieHandler = new NativeCookieHandler ();
var messageHandler = new NativeMessageHandler (false, false, cookieHandler);
cookieHandler.SetCookies (RestApiPaths.cookies);
var client = new HttpClient (messageHandler);
client.DefaultRequestHeaders.TryAddWithoutValidation ("User-Agent", RestApiPaths.GetUserAgent (Constants.__IOS__));
ImageService.Instance.Initialize (new Configuration {
HttpClient = client
});
ImageService.Instance.LoadUrl (url).Into (ArticleImageView);
but unfortunately, it is not working. No image get loaded. I know it is problem with authentication. I tried with another url which does not require authentication and its working. Can someone please tell what should I do to get it working.
EDIT:
I tried this from Authentication-Support
public class AuthenticatedHttpImageClientHandler : HttpClientHandler
{
public AuthenticatedHttpImageClientHandler()
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("Cookie", RestApiPaths.cookie);
request.Headers.Add("User-Agent", RestApiPaths.GetUserAgent(Constants.__IOS__));
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}
then:
ImageService.Instance.Initialize (new Configuration {
HttpClient = new HttpClient(new AuthenticatedHttpImageClientHandler())
});
but still not working..
Here's how I did it
NetworkHelper.GetAuthenticatedHttpClient returns the an HttpClient with the required headers and auth token in my case.
You only have to do
Instance.Initialize
only once