Answered by:
Problems with Live JSON API in C#

Question
-
I have the following Code, which works fine with the flickrUrl, but actually I don't want to access Flickr, but Live. When I change the url in the following line: "wc.DownloadStringAsync(new Uri(flickrUrl + SearchBox.Text));" VS2008 throws an error:
Message: Unhandled Error in Silverlight 2 Application Code: 4004 Category: ManagedRuntimeError Message: System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. ---> System.Security.SecurityException ---> System.Security.SecurityException: Security error. at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState) at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState) --- End of inner exception stack trace --- at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result) --- End of inner exception stack trace --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at System.Net.DownloadStringCompletedEventArgs.get_Result() at WebServices.Page.wc_DownloadFlickrCompleted(Object sender, DownloadStringCompletedEventArgs e) at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
It works with Google API too, but actually I don't want to access Google either. Can someone help me?
public string flickrUrl = "http://api.flickr.com/services/rest/?method=flickr.photos.search&format=json&nojsoncallback=1&api_key=" + flickrKey + "&tags="; public string liveUrl = "http://api.search.live.net/json.aspx?AppId=" + liveSearchKey + "&JsonType=raw&Market=de-DE&sources=image&query="; private void ButtonFlickr_Click(object sender, RoutedEventArgs e) { var wc = new WebClient(); if (!String.IsNullOrEmpty(SearchBox.Text)) { wc.DownloadStringAsync(new Uri(flickrUrl + SearchBox.Text)); } } void wc_DownloadFlickrCompleted(object sender, DownloadStringCompletedEventArgs e) { MessageBox.Show(e.Result, "Debug Info", MessageBoxButton.OK); }
Monday, April 6, 2009 6:56 AM
Answers
-
Hi Sascha,
I am guessing that you are trying to run the silverlight application using the dynamically generated test page in VS 2008 ( option 2 - "Automatically generate a test page to host silverlight at build time" when you try to create the Silverlight application in VS 2008 ). Due to a bug in Silverlight 2 where if you try to do just this AND there is a clientccesspolicy.xml available ( compared to no clientaccesspolicy.xml and a crossdomain.xml file being available) , the securityexception is generated. The reason you are not hitting this exception while using the the flickr Url is because there is no clientaccesspolicy.xml file for flickr.
As a workaround, please use option 1 - "Add a new ASP.NET Web project to the solution to host silverlight" while creating the silverlight application.
HTH
Roopali- Proposed as answer by Roopali kaujalgi - MSFT Wednesday, April 15, 2009 11:34 PM
- Marked as answer by AlessC Tuesday, May 12, 2009 5:40 PM
Wednesday, April 15, 2009 11:32 PM
All replies
-
If you want to access Live Search API 2.0 then the DownloadStringAsync line should be:
wc.DownloadStringAsync(new Uri(liveUrl + SearchBox.Text));
However, you should know that building URLs in this fashion is not secure. A user could easily inject any parameters he/she wishes into the query string, which could cause your application to crash. A better way to write the line is like this:
wc.DownloadStringAsync(new Uri(liveUrl + Uri.EscapeDataString(SearchBox.Text));
Also you should check the value of e.Error to determine if the asynchronous call was successful. Use this MSDN example as reference:
private static void DownloadStringCallback (Object sender, DownloadStringCompletedEventArgs e) { // If the request was not canceled and did not throw // an exception, display the resource. if (!e.Cancelled && e.Error == null) { string textString = (string)e.Result; Console.WriteLine (textString); } }
Eric Carter - SDET - Webmaster Tools TeamTuesday, April 14, 2009 6:09 PM -
Hi Sascha,
I am guessing that you are trying to run the silverlight application using the dynamically generated test page in VS 2008 ( option 2 - "Automatically generate a test page to host silverlight at build time" when you try to create the Silverlight application in VS 2008 ). Due to a bug in Silverlight 2 where if you try to do just this AND there is a clientccesspolicy.xml available ( compared to no clientaccesspolicy.xml and a crossdomain.xml file being available) , the securityexception is generated. The reason you are not hitting this exception while using the the flickr Url is because there is no clientaccesspolicy.xml file for flickr.
As a workaround, please use option 1 - "Add a new ASP.NET Web project to the solution to host silverlight" while creating the silverlight application.
HTH
Roopali- Proposed as answer by Roopali kaujalgi - MSFT Wednesday, April 15, 2009 11:34 PM
- Marked as answer by AlessC Tuesday, May 12, 2009 5:40 PM
Wednesday, April 15, 2009 11:32 PM