微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Silverlight HttpWebRequest SecurityException

问题

我有一个在远程服务器上运行的restful Web服务.我已经制作了一个使用它的WP7应用程序,所以我知道它有效.我正在将应用程序移植到Silverlight Web应用程序并遇到问题.

我已经包含了代码的简化版本以及引发的错误. EndGetResponse方法抛出错误.

随意询问更多信息.我一直在寻找解决方案,但没有发现任何有效或真正适用于我的问题的东西.看起来像这样简单的代码,它在WP7版本上运行得很好.任何帮助将不胜感激.

代码

void SendRequest() {
    string url = "http://some-example-restful-service:5600/locations/data/all";
    HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(url);

    request.Method = "POST";
    request.ContentType = "application/json; charset=utf-8";

    request.BeginGetResponse(new AsyncCallback(RequestCallback), request);
}

public static void RequestCallback(IAsyncResult asyncResult) {
    HttpWebRequest request = (HttpWebRequest) asyncResult.AsyncState;
    HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(asyncResult);

    //Parse JSON object

    response.Close();

    //dispatch result back to GUI thread
}

错误

SecurityException未被用户代码处理

System.Security.SecurityException:安全性错误.

at System.Net.browser.browserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.browser.browserHttpWebRequest.<>c__displayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.browser.AsyncHelper.<>c__displayClass4.<BeginonUI>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 ServieTest.MainPage.RequestCallback(IAsyncResult asyncResult)
at System.Net.browser.browserHttpWebRequest.<>c__displayClassd.<InvokeGetResponseCallback>b__b(Object state2)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

解决方法:

结果我需要在服务域的根目录上添加clientaccesspolicy.xml文件.

以下是文件内容应该是:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="*">
                <domain uri="*"/>
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy> 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐