I am trying to piece together a solution to a problem. Basically I am using Silverlight 4 with C# 4.0 to access the world of warcraft armoury. If anyone has done this - please oh please provide the working .net 4.0 code.
The code I am attempting to run is (e.Error contains a securtiy error):
private void button10_Click(object sender, RoutedEventArgs e)
{
string url = @"http://eu.wowarmory.com/guild-info.xml?r=Eonar&n=Gifted and Talented";
WebClient wc = new WebClient();
// HOW DO I ADD A USER AGENT STRING (RESPONSE MAY VARY (I.E. HTML VS XML) IF PAGE THINKS CALL IS NOT CAPABABLE OF SUPPORTING XML TRANSFORMATIONS)
//wc.ResponseHeaders["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(url)开发者_开发问答);
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
XDocument ArmouryXML = XDocument.Parse(result);
ShowGuildies(ArmouryXML);
}
else
{
MessageBox.Show("Something is complaining about security but not sure what!");
}
}
Notes:
- C# 4.0
- The armoury is an XML file - but i believe it reverts to html should the request not be from a browser that supports XML transformation. But i don't think I am getting this far.
- The armoury has a cross domain policy file on it - this may be the cause of the error (not sure!
- I have uploaded to a production server
- I am testing it locally using IIS website
- I am going insane!
- Websites have made the suggestion that this problem can be overcome by creating a WebProxy - but I haven't the first clue how to do this.
It would be great if someone could take on this challenge and show us all that it is possible. I'd prefer a non-proxy solution first, then try a proxy.
The error details:
e.Error = {System.Security.SecurityException ---> System.Security.SecurityException: Security error. at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.
Any intelligent master coders out there who can solve this in their sleep?
Thanks if you can help.
When running a Silverlight application in a browser, the silverlight application may only make requests to URLs from the same domain as the application was installed from. For instance, if your silverlight app is installed from http://yoursite.com/yourapp.xap
, you can only make WebClient requests to other URLs on http://yoursite.com/
. Your options here are either create a proxy script that requests the WoW armory from your server, and have your silverlight app hit that proxy, or run the silverlight out-of-browser and request full trust.
Edit: Generally the best option is a cross-domain policy file as explained here. Sadly, It does not appear that wowarmory.com implements the cross-domain policy file.
you were on the right track, you just need to set the user-agent.
private void button10_Click(object sender, RoutedEventArgs e)
{
string url = @"http://eu.wowarmory.com/guild-info.xml?r=Eonar&n=Gifted and Talented";
WebClient wc = new WebClient();
// HOW DO I ADD A USER AGENT STRING (RESPONSE MAY VARY (I.E. HTML VS XML) IF PAGE THINKS CALL IS NOT CAPABABLE OF SUPPORTING XML TRANSFORMATIONS)
//wc.ResponseHeaders["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(url));
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
XDocument ArmouryXML = XDocument.Parse(result);
ShowGuildies(ArmouryXML);
}
else
{
MessageBox.Show("Something is complaining about security but not sure what!");
}
}
SOLVED - used a WCF service to act as a proxy due to cross domain security and header modification restrictions. Thanks for all the advice.
精彩评论