I've seen several posts relating to this, and after some investigation I thought I'd post the code here for anyone else that had this issue. System.Net.HttpWebRequest allows remote requests to domains without a cross access policy file (which WebClient
apparently does require). This simple sample code downloads an rss feed and populates a Listbox with the results.
I don't think this will work. The idea behind cross domain access constraints is to enforce security. Not being able to make cross domain calls with the WebClient, but making it this easy with another object wouldn't be a feature.. it would be a bug, a security
flaw.
Actually, the ability to make cross domain requests is extremely important, and Silverlight would be crippled without it. I understand the security aspect for formal webservices, but it's still very important to be able to make custom requests yourself.
For example, any site the publishes an rss feed, but doesn't have a cross domain security policy file.
Even the example rss reader tutorial on this site fails out of box due to this, because it uses WebClient.
I originally had the issue of not being able to find the HttpWebRequest class as well; be sure you add it to the "using" block at the beginning of your codebehind .cs file, AND reference it in the solution explorer in Visual Studio. You will probably have
to then rebuild the project for the references to update, then it should show up.
Certainly. In the callback method, response.GetResponseStream() is read into an XmlReader, but you just as easily could replace that line with Stream mystream = response.GetResponseStream(), at which point you could manipulate it in any way that SL supports.
factor
Member
8 Points
5 Posts
Solution for Cross-Domain requests
Mar 11, 2008 06:23 PM | LINK
I've seen several posts relating to this, and after some investigation I thought I'd post the code here for anyone else that had this issue. System.Net.HttpWebRequest allows remote requests to domains without a cross access policy file (which WebClient apparently does require). This simple sample code downloads an rss feed and populates a Listbox with the results.
private void Start(Uri MyRssUri){
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(MyRssUri);
request.BeginGetResponse(new AsyncCallback(responseHandler), request);
}
void responseHandler(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
XmlReader reader = XmlReader.Create(response.GetResponseStream());
SyndicationFeed feed = SyndicationFeed.Load(reader);
//NewsItems is a Listbox
NewsItems.ItemsSource = feed.Items;
}
cross-domain webservices HttpWebRequest HttpRequest "Silverlight 2" Silverlight 2.0 Beta 1 cross domain request
johnnystock
Contributor
2295 Points
362 Posts
Re: Solution for Cross-Domain requests
Mar 11, 2008 08:12 PM | LINK
Am I missing something but I dont have HttpWebRequest in my System.Net namespace, or any other namespace that I can think of.
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action
gdogg
Member
25 Points
17 Posts
Re: Solution for Cross-Domain requests
Mar 11, 2008 08:21 PM | LINK
its not inlcuded in the Framework for Silverlight, but it is for the "regular" System.Net namespace:
http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
gdogg
Member
25 Points
17 Posts
Re: Solution for Cross-Domain requests
Mar 11, 2008 08:22 PM | LINK
or wait....am I missing something too?
johnnystock
Contributor
2295 Points
362 Posts
Re: Solution for Cross-Domain requests
Mar 11, 2008 08:33 PM | LINK
The Silverlight Docs say it's in the System.Net namespace but the compiler jsut throws build errors.
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action
watchman
Member
36 Points
19 Posts
Re: Re: Solution for Cross-Domain requests
Mar 11, 2008 09:41 PM | LINK
I don't think this will work. The idea behind cross domain access constraints is to enforce security. Not being able to make cross domain calls with the WebClient, but making it this easy with another object wouldn't be a feature.. it would be a bug, a security flaw.
factor
Member
8 Points
5 Posts
Re: Re: Re: Solution for Cross-Domain requests
Mar 11, 2008 10:55 PM | LINK
Actually, the ability to make cross domain requests is extremely important, and Silverlight would be crippled without it. I understand the security aspect for formal webservices, but it's still very important to be able to make custom requests yourself. For example, any site the publishes an rss feed, but doesn't have a cross domain security policy file.
Even the example rss reader tutorial on this site fails out of box due to this, because it uses WebClient.
I originally had the issue of not being able to find the HttpWebRequest class as well; be sure you add it to the "using" block at the beginning of your codebehind .cs file, AND reference it in the solution explorer in Visual Studio. You will probably have to then rebuild the project for the references to update, then it should show up.
BeSilver
Member
76 Points
38 Posts
Re: Solution for Cross-Domain requests
Mar 11, 2008 11:40 PM | LINK
You've got to manually add the assembly and add a Using statement. I've pointed that out in a Silverlight tutorial.
blogging @ www.24100.net
MarkRiemann
Member
97 Points
69 Posts
Re: Solution for Cross-Domain requests
Mar 12, 2008 12:17 AM | LINK
Any way this code could be altered to grab any type of file (binary/non-binary) with the support for cross-domain?
factor
Member
8 Points
5 Posts
Re: Solution for Cross-Domain requests
Mar 12, 2008 12:27 AM | LINK
Certainly. In the callback method, response.GetResponseStream() is read into an XmlReader, but you just as easily could replace that line with Stream mystream = response.GetResponseStream(), at which point you could manipulate it in any way that SL supports.