views:

1304

answers:

2

Not really sure why I'm getting the exception.. the code is sound and the API is working through my browser.

var url = new Uri("http://octopart.com/api/search?keywords=" + topic.Text);
        WebClient octopartCall = new WebClient();
        octopartCall.OpenReadCompleted += new OpenReadCompletedEventHandler(Octopart_Completed);
        octopartCall.OpenReadAsync(url);
 if (e.Error == null)
        ... error is not null so I throw the message below

System.Exception occurred Message=System.Security.SecurityException ---> System.Security.SecurityException: Security error. at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState) at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.b__0(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.OpenReadAsyncCallback(IAsyncResult result) StackTrace: at Register.Page.Octopart_Completed(Object sender, OpenReadCompletedEventArgs e) InnerException:

A: 

Where is error being set? In the Completed handler?

If that's the case, then your problem may be that the handler is not done yet.

Also, you may need to URL encode your URL, since you're adding arbitrary text to it.

Finally, where is this running? ASP.NET? SilverLight? And what versions?

John Saunders
Running in silverlight 3.0. The exception is coming from in my completed event handler at the if e.Error == null void Octopart_Completed(object sender, OpenReadCompletedEventArgs e) { if (e.Error == null) { using (Stream responseStream = e.Result) {//lots of work in here}} else { throw new Exception(e.Error.ToString()); }
rideon88
Please do not throw exceptions in that manner. Instead, `throw new Exception("Exception found in Octopart_Completed", ex);` This will preserve the stack trace and the chain of inner exceptions.
John Saunders
+2  A: 

The Visual Studio built in web server can't handle cross domain connections.

Solution 1: make a new virtual directory for your project and run in IIS Solution 2: add crossdomain.xml configuration file to your website project containing

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
     "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"&gt;
<cross-domain-policy>  
    <allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
rideon88