views:

251

answers:

1

The problem

I have a page with a silverlight object. It attempts to retrieve XML from another (external cross domain) page. But I am struggling with a security exception.

I have this code working brilliantly in WPF.

When using a website hosting a silverlight application with the same code, the user agent string of the HttpRequest object is null (and seemingly cannot be set). In fact there is no header information at all - this causes a security exception when attempting to make my asynchronous call.

The question

Why is the user-agent string (and header information) null in my silverlight 4 application when making an asynchronous call using HttpWebRequest?

Thanks in advance!

A: 

hi, sounds like you are might be hitting a cross domian issue does your external xml have a clientaccesspolicy file or a crossdomain file on the site? http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx

Also what is the exception ? can you post.

Update:

OK, Have looked and run your code and yes it a crossdomian issue. The site "http://eu.wowarmory.com/" has a crossdomian file but only allow access from the sites listed in that file ? is you site one of those ? If not you are hitting crossdomain security. (WPF runs on the full CLR and so does not have these restrictions.)

There are options ... -Silverlight 4 running in elevated trust is one - but that is OOB

-Doing it in Javascript and the passing the data into Silverlight

-Get you server to do the work for you i.e. proxy

-Get them to change the crossdomain file !

There are others it really depends on what you are trying to achieve overall? can you explain more ?

76mel
We are almost there I think but I do get a security error now (even if I upload to a production server). The most internal error simple says "security error". Not tremendously useful! There is a cross domain policy file in place that may cause this - but not sure how to get round it!? at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState) at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
Dan B
I am attempting to access the world of warcraft armoury but the problem is that the browser headers are empty and I'm not sure I understand why. Stephan's post above seems interesting but I'm struggling to understand what he means.
Dan B
In SL3 onward you can do request on the browser stack or on the client stack. They have different features certianly around headers.a really good primer is here http://www.wintellect.com/CS/blogs/jprosise/archive/2009/10/14/silverlight-3-s-new-client-networking-stack.aspxMay I sugeest you post you code and the cossdomain file that exists on the server .. ther crossdomain file has to be on the web server that you are calling i.e world of warcraft armoury server.
76mel
I'll do some research on that thanks. You can view the code and the cross domain file at my full post http://forums.silverlight.net/forums/t/183993.aspx
Dan B
I have it running in WPF. The example in that link to the silverlight forum provides working app - basically a list box, button, a reference to xml.linq and works great - retrieves a list of guild memebers. There is a cross domain - but in this case, I don't think that is an issue - it doesn't quite get that far. There is a security restriction locally I think. Visit the link, give it a go in WPF (literally 5 mins it takes) and then port to Silverlight. I REALLY APPRECIATE YOUR HELP!
Dan B
Ok I see you code now .. will check it out ... will get back to you ..
76mel
OK have added an update for you ...
76mel
Super!!! Excited now :) I read your information on client vs browser. I'm guessing client will work as it does in WPF but I'd prefer browser because of the caching of e.g. images.
Dan B
I don't see the response. Maybe it takes a few minutes to come through - I will keep an eye on it :)
Dan B
I have attached it to the orginal answer as it was a liitle big for a comment
76mel
i see it - me silly! I read now :D
Dan B
Thanks for looking at this and trying it out.I think the best option would be to setup a proxy as you suggest.Can you give me some guidance on this or just a few key phrases I can find out more. I.e. what type of service is it? What kind of service do I create - .ws file? asmx? etc.. Is the idea that you asynchronously call your own server and it's the proxy service on your own server server that makes the HttpWebRequest externally which runs my WPF style client type code? I'm trying to understand the security restrictions so I don't ask any more daft questions :)THANK YOU!
Dan B
Also (don't forget to answer above as well)...it is possible to write a JSON request that is called by Silverlight to retrieve the XML from the site? Isn't this just the same as a HttpWebRequest (browser) request?
Dan B
The purpose of my app at this stage is simply to retrieve a list of guild member character names. Once I have done this I can read any content from the world of warcraft armoury website. Then I can create fancy app (may porting to Windows 7 mobile later on)
Dan B
Ok you need to add a WCF Service to your Web Application. You want that operation call say GetXMLData() to go out to your Warcraft site and get the data for you and. In GetXMLData you write you Http code similar to your WPF code. In you silverlight project you can add a service reference to you newly created WCF Service and code against that new reference calling GetXMLData etc.
76mel
Perfect!! Thank you so much for your help 76mel!!You are a gem - as soon as I get some rep - i'll come back and give you some kudos! :) I'll mark as answer until then. Thank you!
Dan B
Ok, I have just tested it it all seems to work it poulates the list box with names and pop up a message box saying 122 if you ping me your email address I can send you the zip ..
76mel
Nice. And how do I 'ping' you without telling the entire world too?
Dan B
use the email address here http://www.knowledgemill.com/contact-us-2
76mel
Sent now with subject line "Stack overflow WCF example request".Many thanks!
Dan B
SOLVED - used a WCF service to act as a proxy due to cross domain security and header modification restrictions. Thanks for all the advice.
Dan B