当前位置:编程学习 > C#/ASP.NET >>

调哭了!Silverlight已添加clientaccesspolicy.xml,但仍报安全性异常

我在WebService的网站根目录下已经添加了clientaccessplicy.xml文件,但是silverlight中用webhttprequest访问时依然报安全性错误。

webservice在浏览器中访问时正常的。

这是silverlight中访问的的方法:

        private void requestUrl(string url)
        {
            HttpWebRequest request = HttpWebRequest.CreateHttp(url);
            request.Method = "POST";
            request.AllowReadStreamBuffering = true;
            request.ContentType = "application/json";
            request.BeginGetRequestStream(new AsyncCallback(requestReady), request);
        }
 
        private void requestReady(IAsyncResult asyncResult)
        {
            HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
            StreamWriter postStream = new StreamWriter(request.EndGetRequestStream(asyncResult));
            postStream.Write("userName=name");
            postStream.Close();
            postStream.Dispose();
            request.BeginGetResponse(new AsyncCallback(responseReady), request);
        }
 
        private void responseReady(IAsyncResult asyncResult)
        {
            HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
            WebResponse response = request.EndGetResponse(asyncResult);
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            textBlock.Text = reader.ReadToEnd();
            response.Close();
            responseStream.Close();
            responseStream.Dispose();
            reader.Close();
            reader.Dispose();
        }


附上webservice中的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>
silverlight安全性错误 silverlight跨域 --------------------编程问答-------------------- 你先找一个http调试器看看silverlight在第一次访问webservice之前的两次访问授权xml文件的http请求、已经返回的404消息到底是什么?!

贴一堆这类东西只能说明是乱拼凑起来的。 --------------------编程问答-------------------- 不知道哪边报错了,没法看 --------------------编程问答--------------------
引用 1 楼 sp1234 的回复:
你先找一个http调试器看看silverlight在第一次访问webservice之前的两次访问授权xml文件的http请求、已经返回的404消息到底是什么?!

贴一堆这类东西只能说明是乱拼凑起来的。


对不起,我刚试了一下,clientaccesspolicy.xml正常加载,404错误是:
Resource interpreted as Image but transferred with MIME type text/html: "http://192.168.80.2/". HttpRequestSampleTestPage.html:20
Uncaught Error: Unhandled Error in Silverlight Application 远程服务器返回了错误: NotFound。   位于 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   位于 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   位于 HttpRequestSample.MainPage.responseReady(IAsyncResult asyncResult)
   位于 System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2)
   位于 System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   位于 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   位于 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   位于 System.Threading.ThreadPoolWorkQueue.Dispatch()
   位于 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()


请问这可能是什么原因呢? --------------------编程问答--------------------
引用 2 楼 qiujialongjjj 的回复:
不知道哪边报错了,没法看


嗯,我刚发了报的错误 --------------------编程问答--------------------
引用 4 楼 fjzpwang 的回复:
Quote: 引用 2 楼 qiujialongjjj 的回复:

不知道哪边报错了,没法看


嗯,我刚发了报的错误
--------------------编程问答-------------------- 原来无法请求的问题还是没解决,晚上尝试使用wcf ria 来请求数据,发现请求.asmx服务的时候返回500,但是请求.aspx的时候正常,还是请教前辈们!
这是.asmx文件的代码,是不是有错?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using Newtonsoft.Json;

/// <summary>
///AlarmService 的摘要说明
/// </summary>
[WebService(Namespace = "http://**.**.**/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public class AlarmService : System.Web.Services.WebService
{

    public AlarmService()
    {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public string GetAlarmMsg(string userName)
    {
        return "result";
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public string AddAlarmMsg()
    {
        return "";
    }
}

--------------------编程问答--------------------
引用 3 楼 fjzpwang 的回复:
Quote: 引用 1 楼 sp1234 的回复:

你先找一个http调试器看看silverlight在第一次访问webservice之前的两次访问授权xml文件的http请求、已经返回的404消息到底是什么?!

贴一堆这类东西只能说明是乱拼凑起来的。


对不起,我刚试了一下,clientaccesspolicy.xml正常加载,404错误是:
Resource interpreted as Image but transferred with MIME type text/html: "http://192.168.80.2/". HttpRequestSampleTestPage.html:20
Uncaught Error: Unhandled Error in Silverlight Application 远程服务器返回了错误: NotFound。   位于 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   位于 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   位于 HttpRequestSample.MainPage.responseReady(IAsyncResult asyncResult)
   位于 System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2)
   位于 System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   位于 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   位于 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   位于 System.Threading.ThreadPoolWorkQueue.Dispatch()
   位于 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()


请问这可能是什么原因呢?


晕啊!

你没有搞懂我说的“http调试器”的概念吗?

那么给你一个具体的名词,例如 Fiddler。启动这个程序,然后你再用silverlight程序访问,你会看到silverlight程序首先会访问它自己授权文件,如果出错就会再访问Flash的授权文件。

你可以看到两次访问的url路径。然后看看你部署的clientaccessplicy.xml的路径是否正确。

这里所说的是使用http调试器,而不是vs!不要仅贴出一堆vs调试信息,要学会使用正确的调试方法。 --------------------编程问答-------------------- 学会“方法”,你自己就会基本的调试,不需要求助csdn。

方法不对,那么“百忙”而毫无效果的“瞎试”的无奈举动,一眼就让人看出来了。 --------------------编程问答--------------------
引用 6 楼 fjzpwang 的回复:
原来无法请求的问题还是没解决,晚上尝试使用wcf ria 来请求数据,发现请求.asmx服务的时候返回500,但是请求.aspx的时候正常,还是请教前辈们!
这是.asmx文件的代码,是不是有错?


根据原来的请求具体的url,才能知道你应该在服务器端哪一个程序中设置断点调试。因此调试http通讯过程,你需要首先能够“贴出来”(说出来)总共发送了几条http消息,分别是什么消息,哪一条消息对应哪一个服务端的代码。

补充:.NET技术 ,  Web Services
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,