首页 文章

C#JSON WebClient请求抛出ex

提问于
浏览
1

不确定为什么我得到异常..代码是合理的,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

发生System.Exception Message = System.Security.SecurityException ---> System.Security.SecurityException:安全性错误 . System.Net上的System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult),System.Net.Browser.AsyncHelper上的<> c__DisplayClass5.b__4(Object sendState) . <> c__DisplayClass2.b__0(Object sendState) ---内部异常堆栈跟踪的结束---位于System.Net.WebClient的System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Object state) . System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult结果)中的GetWebResponse(WebRequest请求,IAsyncResult结果)StackTrace:at Register.Page.Octopart_Completed(Object sender,OpenReadCompletedEventArgs e)InnerException:

2 回答

  • 2

    Web服务器中内置的Visual Studio无法处理跨域连接 .

    解决方案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>
    
  • 0

    error 在哪里设置?在 Completed 处理程序中?

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

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

    最后,这是在哪里运行? ASP.NET? SilverLight的?什么版本?

相关问题