我对位于 Facebook 照片服务器上的 crossdomain.xml 有疑问。第一个问题出现在 Silverlight 请求 clientaccesspolicy.xml 时——Facebook 服务器返回 403——访问被拒绝,这很好,因为他们的服务器上也部署了 crossdomain.xml。 Silverlight 然后请求那个 crossdomain.xml,它得到的响应正是这样的:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain- policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" secure="false" to-ports="*" />
<site-control permitted-cross-domain-policies="master-only" />
</cross-domain-policy>
然后我玩了一会儿,将 crossdomain.xml 部署到我自己的服务器上,得到了相同的结果——一个安全异常。然后我开始把事情搬出去并得出结论,如果我只删除 to-ports="*" 属性,一切都会按预期工作?有没有人知道如何克服这个问题,以前有没有人遇到过同样的问题,或者这是我做错了什么?
最佳答案
我在尝试以编程方式从 Facebook 检索图像时遇到了同样的问题。奇怪的是,如果您将 Silverlight 图像控件指向 Facebook 图像 url,图像将被检索并正确显示。这让我开始思考,我想出了一个可行的解决方法,似乎对我的情况一直有效。我希望你也觉得它有用。
var uri = new Uri("http://graph.facebook.com/mglace/picture/", UriKind.Absolute);
var bmp = new BitmapImage();
bmp.ImageOpened += (sender, e) => { /* Do something here with the sender (which is the BitmapImage) */ };
bmp.CreateOptions = BitmapCreateOptions.None;
bmp.UriSource = uri;
创建一个 BitmapImage
对象,为 ImageOpened
事件设置事件处理程序并将 CreateOptions
属性设置为 BitmapCreateOptions.None
。最后,将 UriSource
设置为您要检索的 Facebook 图片。图片会立即下载,因为我们将 CreateOptions
设置为 None
(默认值为 DelayedCreation
)。然后,您可以在 ImageOpened
事件处理程序中执行任何您想要的操作。
我想将这个逻辑封装在我的服务层中并加强错误处理等,所以我将它包装在一个 Reactive Extensions observable 中以使其更易于使用。这是我的最终代码片段:
public IObservable<BitmapImage> GetProfilePhoto(string profileId)
{
return Observable.Create<BitmapImage>(
observer =>
{
// This handler handles a successful fetch
EventHandler<RoutedEventArgs> openedHandler =
(sender, args) =>
{
try
{
observer.OnNext(sender as BitmapImage);
observer.OnCompleted();
}
catch (Exception ex)
{
observer.OnError(ex);
}
};
// This handler handle a failure
EventHandler<ExceptionRoutedEventArgs> failedHandler =
(sender, args) => observer.OnError(args.ErrorException);
var url = string.Format("http://graph.facebook.com/{0}/picture/", profileId);
var uri = new Uri(url, UriKind.Absolute);
BitmapImage bmp = null;
try
{
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
bmp = new BitmapImage();
bmp.ImageOpened += openedHandler;
bmp.ImageFailed += failedHandler;
bmp.CreateOptions = BitmapCreateOptions.None;
bmp.UriSource = uri;
});
}
catch (Exception ex)
{
observer.OnError(ex);
}
return () =>
{
// Cleanup the event handlers
if (bmp != null)
{
bmp.ImageOpened -= openedHandler;
bmp.ImageFailed -= failedHandler;
}
};
});
}
和用法:
GetProfilePhoto("mglace")
.Subscribe(image => { /* Do something with the image in here*/ },
error => { /* Handle any errors in here */ },
() => { /* Finalization code goes here */ });
我希望有人觉得这很有用。
关于silverlight - Facebook crossdomain.xml silverlight 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6818078/