我有一个包含https和http端点的SL应用。如果我在http上访问端点,那么只需要一个加载外部图像的屏幕
http://somedomain.com/domaimage.jpg
它会正常工作。
如果我在https://上访问SL应用程序,然后加载相同的图像,它甚至不会尝试为图像发出Web请求。
为什么SL在https上运行时我不会请求外部内容?我在clientaccesspolicy.xml
中有这个 <?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*" http-methods="*">
<domain uri="http://*"/>
<domain uri="https://*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
这是我的crossdomain.xml
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
由于
史蒂夫
答案 0 :(得分:0)
现在使用图片代理访问外部内容,如果它可以帮助任何人
ImageProxy.ashx
public class ImageUrlProxy : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string url = context.Request.Headers["Url"];
var client = new WebClient();
byte[] imageDataBytes = client.DownloadData(url);
context.Response.ContentType = "application/json;";
context.Response.Write(JsonConvert.SerializeObject(imageDataBytes));
}
catch (Exception)
{
throw;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
助手类:
public static class ImageProxyHelper
{
public static void GetImageByProxy(string url, Action<BitmapImage> callback)
{
if (callback == null) return;
var client = new WebClient();
client.DownloadStringCompleted += (sender, args) =>
{
if (args.Error == null)
{
var buffer = JsonConvert.DeserializeObject<byte[]>(args.Result);
var im = new BitmapImage() { CreateOptions = BitmapCreateOptions.None };
im.SetSource(new MemoryStream(buffer));
callback(im);
}
};
client.Headers["Url"] = url;
client.DownloadStringAsync(UrlBuilder("ImageUrlProxy.ashx"));
}
public static Uri UrlBuilder(string fragment)
{
var uriBuilder = new UriBuilder(Application.Current.Host.Source.Scheme,
Application.Current.Host.Source.Host,
Application.Current.Host.Source.Port, fragment);
return uriBuilder.Uri;
}
}
然后来自silverlight:
ImageProxyHelper.GetImageByProxy("http://externaldomain.com/image.jpg", p=>{
//Do something here
})
这可以扩展为返回任何外部内容^^
答案 1 :(得分:0)