FFImageLoading:如何添加身份验证?
FFImageLoading: How to add authentication?
我正在尝试在我的 Xamarin.iOS 和 Xamarin.Android 应用程序中使用 FFImageLoading 从 url 加载图像。所有 url 都需要身份验证..
我还使用 modernhttpclient
进行所有其他休息呼叫。对于 modernhttpclient
这就是我添加身份验证的方式
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));
而且效果很好。我对 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);
但不幸的是,它不起作用。没有图像被加载。我知道这是身份验证的问题。我尝试了另一个 url 不需要身份验证和它的工作。谁能告诉我应该怎么做才能让它工作。
编辑:
试过这个
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);
}
}
然后:
ImageService.Instance.Initialize (new Configuration {
HttpClient = new HttpClient(new AuthenticatedHttpImageClientHandler())
});
但还是不行..
这是我的做法
ImageService.Instance.Initialize(new Configuration {
HttpClient = NetworkHelper.GetAuthenticatedHttpClient(Constants.__IOS__)
});
ImageService.Instance.LoadUrl(url).Into(imageView);
NetworkHelper.GetAuthenticatedHttpClient returns 在我的例子中,具有所需 headers 和身份验证令牌的 HttpClient。
public static HttpClient GetAuthenticatedHttpClient(int platform)
{
HttpClient client = new HttpClient(messageHandler);
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", GetUserAgent(platform));
client.DefaultRequestHeaders.TryAddWithoutValidation("X-Time-Zone", timezone);
client.DefaultRequestHeaders.TryAddWithoutValidation("appVersion", CommonHelper.AppVersion);
client.DefaultRequestHeaders.TryAddWithoutValidation("X-Auth", GetAuthToken());
return client;
}
你只需Instance.Initialize
一次
我正在尝试在我的 Xamarin.iOS 和 Xamarin.Android 应用程序中使用 FFImageLoading 从 url 加载图像。所有 url 都需要身份验证..
我还使用 modernhttpclient
进行所有其他休息呼叫。对于 modernhttpclient
这就是我添加身份验证的方式
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));
而且效果很好。我对 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);
但不幸的是,它不起作用。没有图像被加载。我知道这是身份验证的问题。我尝试了另一个 url 不需要身份验证和它的工作。谁能告诉我应该怎么做才能让它工作。
编辑:
试过这个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);
}
}
然后:
ImageService.Instance.Initialize (new Configuration {
HttpClient = new HttpClient(new AuthenticatedHttpImageClientHandler())
});
但还是不行..
这是我的做法
ImageService.Instance.Initialize(new Configuration {
HttpClient = NetworkHelper.GetAuthenticatedHttpClient(Constants.__IOS__)
});
ImageService.Instance.LoadUrl(url).Into(imageView);
NetworkHelper.GetAuthenticatedHttpClient returns 在我的例子中,具有所需 headers 和身份验证令牌的 HttpClient。
public static HttpClient GetAuthenticatedHttpClient(int platform)
{
HttpClient client = new HttpClient(messageHandler);
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", GetUserAgent(platform));
client.DefaultRequestHeaders.TryAddWithoutValidation("X-Time-Zone", timezone);
client.DefaultRequestHeaders.TryAddWithoutValidation("appVersion", CommonHelper.AppVersion);
client.DefaultRequestHeaders.TryAddWithoutValidation("X-Auth", GetAuthToken());
return client;
}
你只需Instance.Initialize
一次