Paul Kinlan on August 14th, 2008

Silverlight 2 Beta 2 Bug?

I have been developing a silverlight twitter client using the Twitter API but have been having a couple of problems being able to perform cross domain requests via the WebClient API.

The following code is the code that can be used to replicate the issue.  I have had to encode a http header for authorization as the Silverlight WebClient doesn’t directly support authentication.

 

WebClient webclient = new WebClient();
Byte[] authbytes = UTF8Encoding.UTF8.GetBytes("UsernameHidden:PasswordHidden");
webclient.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authbytes);
webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
webclient.DownloadStringAsync(new Uri("http://twitter.com/statuses/friends_timeline.xml"));

 

The result is that an exception occurs in the webclient_DownloadStringCompleted method when trying to access the Result data.  The Exception is a rather cryptic "

{System.Net.WebException: An exception occurred during a WebClient request. —> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.

   at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)

   at System.Windows.DependencyObject.MethodEx(String methodName, CValue[] cvData)

   at MS.Internal.InternalWebRequest.Send()

   at System.Net.BrowserHttpWebRequest.BeginGetResponseImplementation()

   at System.Net.BrowserHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)

   at System.Net.AsyncHelper.BeginOnUI(BeginMethod beginMethod, AsyncCallback callback, Object state)

   at System.Net.BrowserHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)

   at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)

   at System.Net.WebClient.DownloadStringAsync(Uri address, Object userToken)

   — End of inner exception stack trace —}

"

I *think* it is to do with using the authorization header in the WebClient.  When the Authorization header is not specified the silverlight app will connect to Twitter, attempt to download the clientaccesspolicyfile.xml (which fails as it is not on Twitter), it will attempt to download the crossdomain.xml file (which succeeds) however because we are not authenticated on Twitter the request will return a blank string.

When the header is present, the above exception is thrown (well attached to the result property) in the DownloadStringCompleted event handler.  Silverlight doesn’t even attempt to get the policy files.

Not Chuffed!, however I will try normal HttpWebRequests to see if that is any different.  But it looks like it is not possible to use Basic Authentication on cross domain requests (not too sure if it works locally).

 

Uncategorized 1 Comment

One Response to “Silverlight 2 Beta 2 Bug?”

  1. on 15 Aug 2008 at 9:25 pm # Peter Bromberg

    The “Authorization” header is unfortunately on the “restricted” list. See here: http://msdn.microsoft.com/en-us/library/system.net.webheadercollection(VS.95).aspx

Trackback URI Comments RSS

Leave a Reply