Xamarin Android Url图像获取请求接收403错误,但不在iOS上
我正在开发一个xamarin应用程序,它将图像存储在S3存储桶中。使用正确构造的Url时,查询在xamarin中正常工作:Xamarin Android Url图像获取请求接收403错误,但不在iOS上,android,xamarin,xamarin.android,imageview,android-imageview,Android,Xamarin,Xamarin.android,Imageview,Android Imageview,我正在开发一个xamarin应用程序,它将图像存储在S3存储桶中。使用正确构造的Url时,查询在xamarin中正常工作: https:// + BucketName + path + ".jpg?AWSAccessKeyId=keycode&Expires=expireNumber&Signature=signatureCode" 使用时 Image.Source=urlAddress(如上格式) 图像加载良好 部分应用程序页面具有自定义渲染器,其中包含需要通过url地址
https:// + BucketName + path + ".jpg?AWSAccessKeyId=keycode&Expires=expireNumber&Signature=signatureCode"
使用时
Image.Source=urlAddress
(如上格式)
图像加载良好
部分应用程序页面具有自定义渲染器,其中包含需要通过url地址渲染的图像。我们在每个操作系统级别通过url更新图像。iOS正在使用以下代码正常工作:
using (var url = new NSUrl(uri))
using (var data = NSData.FromUrl(url))
if (data != null)
return UIImage.LoadFromData(data);
它成功地从Url获取图像并对其进行更新。然而,我有一个主要的问题,让它在Android上工作。我尝试了以下方面:
制作一个基本的android url,并使用以下代码设置imageView。这在这里被解释为不起作用
SetImageURI(url)
在同一链接上,用户“rmacias”建议使用WebClient通过url下载数据,并将字节解析为android位图
private Bitmap GetImageBitmapFromUrl(string url){
Bitmap-imageBitmap=null代码>
using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;}
这将返回403禁止的错误。在行var imageBytes=webClient.DownloadData(url)
但是,同样的过程在iOS中工作,字符串已经过身份验证,我已经设置了几分钟的身份验证超时,以防加载缓慢。我还在.Net.Http
库中使用了相同的url请求方法。
它在res=(HttpWebResponse)请求时崩溃代码>具有相同的403禁止错误
我尝试了多种方法,对WebClient和Http客户端进行头身份验证。它觉得android请求url数据有点特别,因为url字符串中的身份验证适用于Xamarin图像和ioS代码
我在想,我缺少的是安卓特有的东西?非常感谢您的帮助 如何使用HttpClient,它可以利用Xamarin提供的特定于平台的HttpClientHandler
比如:
// make sure to reuse your HttpClient instance, it is a shared resource
// using it in a using() and disposing it all the time, will leave
// sockets open and bog down the connection!
private static HttpClient _httpClient;
public async Task<byte[]> GetImageDataAsync(string url)
{
if (_httpClient == null)
{
// you could inject AndroidHttpClientHandler or NSUrlSessionHandler here...
_httpClient = new HttpClient();
// set headers etc...
}
var response = await _httpClient.GetAsync(url).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
return null;
var result = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
return result;
}
在iOS上
var data = await GetImageDataAsync(url);
var imageData = NSData.FromArray(data);
imageBitmap = UIImage.LoadFromData(imageData);
还有一些很好的库,比如FFixeloOutlook,它支持这一点,具有效果,在表视图中加载图像等等,您可以将其视为备选方案。
< P>如何使用HTTPclipse,如何利用XAMARIN提供的特定平台的HTTPclieNDENLLE?
比如:
// make sure to reuse your HttpClient instance, it is a shared resource
// using it in a using() and disposing it all the time, will leave
// sockets open and bog down the connection!
private static HttpClient _httpClient;
public async Task<byte[]> GetImageDataAsync(string url)
{
if (_httpClient == null)
{
// you could inject AndroidHttpClientHandler or NSUrlSessionHandler here...
_httpClient = new HttpClient();
// set headers etc...
}
var response = await _httpClient.GetAsync(url).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
return null;
var result = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
return result;
}
在iOS上
var data = await GetImageDataAsync(url);
var imageData = NSData.FromArray(data);
imageBitmap = UIImage.LoadFromData(imageData);
<> P>还有不错的库,比如FFixeloODIN,它支持这个,包括效果,在表视图中加载图像等等,你可以把它当作一个备选方案。似乎可以很好地使用我的预认证url字符串(因此不需要标题等)。非常好,谢谢!似乎可以很好地处理我的预验证url字符串(因此不需要标题等)。