C#JSON WebClient请求抛出

C#JSON WebClient请求抛出,c#,json,exception,webclient,C#,Json,Exception,Webclient,我不太清楚为什么会有例外。。代码是健全的,API正在通过我的浏览器工作 var url = new Uri("http://octopart.com/api/search?keywords=" + topic.Text); WebClient octopartCall = new WebClient(); octopartCall.OpenReadCompleted += new OpenReadCompletedEventHandler(Octopart_Com

我不太清楚为什么会有例外。。代码是健全的,API正在通过我的浏览器工作

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  
系统。发生异常 Message=System.Security.SecurityException-->System.Security.SecurityException:安全错误。 位于System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) 在System.Net.Browser.BrowserHttpWebRequest.c__DisplayClass5.b__4(对象发送状态)中 在System.Net.Browser.AsyncHelper.c__DisplayClass2.b__0(对象发送状态)中 ---内部异常堆栈跟踪的结束--- 位于System.Net.Browser.AsyncHelper.BeginNoui(SendOrPostCallback beginMethod,对象状态) 位于System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在System.Net.WebClient.GetWebResponse(WebRequest请求,IAsyncResult结果) 在System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult结果)中 堆栈跟踪: 在Register.Page.Octopart_处完成(对象发送方,OpenReadCompletedEventArgs e)
InnerException:

正在设置的
错误在哪里?在
已完成的
处理程序中

如果是这种情况,那么您的问题可能是处理程序尚未完成

此外,您可能需要对您的URL进行URL编码,因为您正在向其中添加任意文本


最后,它在哪里运行?ASP.NET?银灯?什么版本?

Visual Studio内置web服务器无法处理跨域连接

解决方案1:为项目创建一个新的虚拟目录并在IIS中运行 解决方案2:将crossdomain.xml配置文件添加到包含

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


在silverlight 3.0中运行。异常来自if e.Error==null void Octopart_completed(对象发送方,OpenReadCompletedEventArgs e){if(e.Error==null){使用(Stream responseStream=e.Result){//此处有大量工作}}否则{抛出新异常(e.Error.ToString());}请不要以这种方式抛出异常。相反,
抛出新异常(“在Octopart_Completed中发现的异常”,ex);
这将保留堆栈跟踪和内部异常链。