System.Net.WebException with FlickrNetSilverlight 3.2.4310.0

Topics: Developer Forum
Jan 25, 2012 at 7:24 PM
Edited Jan 25, 2012 at 7:51 PM

Good afternoon!

I'm playing around with flickr.net, both in plain old .NET and Silverlight.  I have had great success with .NET, but I'm running in to a problem when I try to use Silverlight.

I created a Silverlight 4 project, added the reference to FlickrNetSilverlight, and plopped the following code in MainPage.xaml:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FlickrNet;

namespace FlickrSilverlightv4
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            Flickr flickr = new Flickr(myAPIkey, mySharedSecret);

            PhotoSearchOptions options = new PhotoSearchOptions();            
            options.Tags = "microsoft";            

            flickr.PhotosSearchAsync(options, r =>
            {
                if (r.HasError)
                {
                }
                else
                {
                    PhotoCollection photos = r.Result;
                }
            });
        }
    }
}

 

When I try to run it, though, I get the following error (ETA: in r.Error.  No results are returned in r.Result):


{System.Net.WebException: An exception occurred during a WebClient request. ---> 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)
   at System.Net.WebClient.UploadStringAsync(Uri address, String method, String data, Object userToken)
   --- End of inner exception stack trace ---}

 

For quick reference, in the Flickr class, the user agent is hardcoded as follows:

public const string UserAgent = "Mozilla/4.0 FlickrNet API (compatible; MSIE 6.0; Windows NT 5.1)";

I am running this application on my localhost using Visual Studio 2010 on Win7 64-bit, with the latest Silverlight developer runtime (5.0.61118.0), and with the most current version of the flickr.net API library.  It fails whether I target Silverlight 4 or 5.

Please let me know if there's any further information I can provide.

Thanks in advance for your help (and for this library - it's fantastic, and saves so much work :) )

Lisa Chesley

Coordinator
Jan 26, 2012 at 8:08 AM

OK, I've removed the user-agent from the library. It was of dubious use anyway.

Sam

Jan 26, 2012 at 1:22 PM

Good morning, Sam!

Thanks so much for that - I've downloaded the latest source code, rebuilt the dll and updated my reference, and that error is no more!  Sadly, though, I'm now getting a security exception - it appears that http://api.flickr.com/clientaccesspolicy.xml does not exist (getting a 404 with fiddler, and when I actually try to navigate to the page), so I'm getting the following error (same code as before):

{System.Security.SecurityException ---> System.Security.SecurityException: Security error.
   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
   at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)}    System.Security.SecurityException

Is that something that Flickr has recently changed, or something I can update myself?

Please let me know if I can provide any further information!

Thanks again for your help!

Lisa Chesley

Coordinator
Jan 27, 2012 at 9:37 AM

Well, that's a helpful message isn't it.

Turns out its got nothing to do with the client access policy. Silverlight checks for it and if it doesn't exist then it falls back to the ccrossdomain.xml file, which does exist.

It turns out that the error is actually cause by setting the Authorization header, which isn't supported in Silverlight. So I've swapped it around to put all OAuth parameters in the body and it now works fine.

Thanks for reporting this. I don't use Silverlight much so I'm not likely to find this kind of issue unless someone points them out to me.

Sam

Jan 27, 2012 at 1:09 PM

Good morning, Sam!

Yes, those error messages are *so* helpful. :)

I've downloaded your changes (thanks again, btw!), built the dlls, and updated my references, but I'm still getting the exact same error message.  Just to be sure I've got the most recent stuff, I download the source from here, and pulled the svn repository down to my pc as well.  Help?

Also, it's my pleasure to help - I'm playing around with the Silverlight for kicks, but I am using the .NET version of the library to build a photo gallery for an organization I'm part of in my off work hours.  They have a lot of photos, so I think a working pivot viewer would blow their minds, if I can get the Silverlight stuff up and running. :)

Thanks again for your help!

Please let me know if I can provide any further information!

Lisa Chesley

Coordinator
Jan 27, 2012 at 1:31 PM

Sorry, I've double checked and it seems to work fine at this end. Could you try clearing your cache? I had an issue where Chrome wasn't loading the latest version for a while, but IE would get the newest version of the app - very annoying.

Sam

Jan 27, 2012 at 2:10 PM

Good morning, Sam!

I ended up creating a fresh solution, but it's now working - thanks again for your help, and for this fabulous library!

Have a great day!

Lisa Chesley