gpt4 book ai didi

c# - Silverlight HttpWebRequest 安全异常

转载 作者:太空狗 更新时间:2023-10-30 00:34:15 24 4
gpt4 key购买 nike

问题

我有一个在远程服务器上运行的 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>

关于c# - Silverlight HttpWebRequest 安全异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8300407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com