1

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:

  1. C# 4.0
  2. 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.
  3. The armoury has a cross domain policy file on it - this may be the cause of the error (not sure!
  4. I have uploaded to a production server
  5. I am testing it locally using IIS website
  6. I am going insane!
  7. 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.

Drazisil
  • 3,070
  • 4
  • 33
  • 53
Dan B
  • 936
  • 2
  • 13
  • 26
  • Can you post the solution used ? ( The implementation of WCF service ) –  May 31 '11 at 11:53
  • Well it would do no point since wow no longer exposes their data via an XML service. Shame - it was a great set of data. – Dan B Jun 02 '11 at 20:31

3 Answers3

3

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.

Community
  • 1
  • 1
Matt Bridges
  • 48,277
  • 7
  • 47
  • 61
  • Sounds good in theory but this code was adapted from Scott Gu's blog, see http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx This example clearly uses digg server on another server. Can you explain your comment compared with this seemly contradictory example? Many thanks! – Dan B May 24 '10 at 21:36
  • Digg implements a cross-domain policy file for silverlight. Wowarmory.com doesn't :( – Matt Bridges May 27 '10 at 04:08
  • Nice to know that about digg :) – Dan B May 27 '10 at 07:01
1

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!");
        }
    } 
Nate
  • 11
  • 1
  • ooooo - so that's how you set the user agent string in .net 4.0. Can't wait to try it - I will definately let you know how it turns out! – Dan B May 25 '10 at 06:30
  • I get an error message stating that the user agent can't be modified directly... + InnerException {System.ArgumentException: The 'User-Agent' header cannot be modified directly. Parameter name: name at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String name, String value) at System.Net.WebHeaderCollection.Add(String name, String value) at System.Net.HttpWebRequest.set_Headers(WebHeaderCollection value) at System.Net.WebClient.CopyHeadersTo(WebRequest request) at System.Net.WebClient.GetWebRequest(Uri address) .... – Dan B May 25 '10 at 17:13
  • Sorry, I did not realize this is a silverlight application. – Nate May 25 '10 at 21:06
  • Last night I tried it in WPF - your code works a treat there and the whole application works as intended but for some reason I can't understand, it doesn't work in Silverlight. – Dan B May 26 '10 at 06:50
  • 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 May 26 '10 at 20:53
1

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
  • 936
  • 2
  • 13
  • 26
  • FYI - WoW now exposes a rest based service so my question and any answers are out of date. The data is returned as JSON. I think you get so many API calls, then you'll maybe want to ask them for extra permission to make large application calls. – Dan B Oct 25 '11 at 08:20