Cross domain request in Silverlight 2.0 beta 2

Silverlight Logo

So you started using Silverlight and you wanted to download a RSS feed, talk to a web service or download a few images from a website to realize that many of the requests you made come back to you empty or you get the 404 error. How do I get the error when I know that web page or image is there?

There are 2 methods in Silverlight to make HTTP requests to a server, both methods WebClient and HttpWebRequest check at the destination domain for the file crossdomain.xml

WebClient webClient = new WebClient();               
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.Downloaded);
webClient.DownloadStringAsync(Uri);
and …
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri);

request.BeginGetResponse(new AsyncCallback(ReadCallback), request); 

There is no a restriction on the browser that prevents Silverlight to do a cross domain request, I do not know why Microsoft still keeps that restriction from the Silverlight plugin as just makes the developer having to create a server side proxy in each application to accomplish those requests.

 

<%@ WebHandler Language="C#" Class="SilverlightProxy" %>

using System;
using System.Web;

public class SilverlightProxy : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        string url = context.Request.QueryString[0];

        System.Net.WebRequest req = System.Net.WebRequest.Create(new Uri(url));
        req.Method = context.Request.HttpMethod;

        if (context.Request.InputStream.Length > 0)
        {
            byte[] bytes = new byte[context.Request.InputStream.Length];
            context.Request.InputStream.Read(bytes, 0, (int)context.Request.InputStream.Length);
            req.ContentLength = bytes.Length;
            req.ContentType = "application/x-www-form-urlencoded";
            System.IO.Stream outputStream = req.GetRequestStream();
            outputStream.Write(bytes, 0, bytes.Length);
            outputStream.Close();
        }

        System.Net.WebResponse response = req.GetResponse();
        context.Response.ContentType = response.ContentType;

        System.IO.StreamReader stream = new System.IO.StreamReader(response.GetResponseStream());

        // Text responses
        if (response.ContentType.Contains("text"))
        {
            string textResponse = stream.ReadToEnd();
            context.Response.Write(textResponse);
        }
        else
        {
            byte[] bResponse = new byte[response.ContentLength];
            stream.BaseStream.Read(bResponse, 0, (int)response.ContentLength);
            context.Response.BinaryWrite(bResponse);        
        }
        context.Response.Flush();
        context.Response.End();       
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

Download the Silverlight proxy here

All your request that are cross domain will have to call the silverlightProxy.ashx file before: SilverlightProxy.ashx?http://silverlightme.net/al.jpg

Flex also work in the same way, is that why Microsoft design it that way? I would think that having socket support from the Silverlight plugin, you would be able to create a socket to a server in TCP IP instead of using HTTP as the protocol.

Cheers

Al

Published Wednesday, September 10, 2008 11:13 PM by albertpascual

Comments

# re: Cross domain request in Silverlight 2.0 beta 2

Tuesday, October 28, 2008 8:57 AM by jcp

Hello Al

This is an interesting idea. Thanks for the posting.

Regards,

Jerry

Leave a Comment

(required) 
(required) 
(optional)
(required)