积极答复者
WebRequest只能访问在同一webContainer中的资源吗?

问题
-
跟随TerryLee的教程一步一步学SL。做到webrequest资源调用时发现一个问题:http://localhost:49955/BookHandler.ashx是我要访问的服务,下面是客户端代码,我发现如果客户端代码和服务端代码在同一个webContainer中(即都在http://localhost:49955下才能正确的返回结果)。但在不同的Container运行(同一台机器上只是端口不同也不行)则不能返回任何结果。传说中的跨域访问问题我已经考虑到了。wwwroot和服务(http://localhost:49955)所在目录下我都放了clientaccesspolicy.xml。实在想不出这是什么问题,都弄了两天了。。。。那位高人知道什么问题?Code Snippet
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
run();
}
//调用web服务
public void run()
{
Uri endpoint = new Uri("http://localhost:49955/BookHandler.ashx");
WebRequest request = WebRequest.Create(endpoint);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}
void RequestReady(IAsyncResult asyncResult)
{
WebRequest request = asyncResult.AsyncState as WebRequest;
Stream requestStream = request.EndGetRequestStream(asyncResult);
using (StreamWriter writer = new StreamWriter(requestStream)) {
writer.Write(String.Format("No={0}", "1"));
writer.Flush();
}
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
}
void ResponseReady(IAsyncResult asyncResult)
{
WebRequest request = asyncResult.AsyncState as WebRequest;
WebResponse response = request.EndGetResponse(asyncResult);
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream);
String resultContent = reader.ReadToEnd();
txt.Dispatcher.BeginInvoke(delegate { ProcessResult(resultContent); });
}
}
//将返回结果更新到UI
private void ProcessResult(string xmlResult)
{
txt.Text = xmlResult;
}
}
答案
-
你应当用 色狼2 beta2开发的吧,同时需要增加一个:
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>祝你成功
- 已标记为答案 微软中文技术论坛Moderator 2009年6月30日 5:27
-
添加
clientaccesspolicy.xml
也可以
<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>- 已标记为答案 微软中文技术论坛Moderator 2009年6月30日 5:27
全部回复
-
你应当用 色狼2 beta2开发的吧,同时需要增加一个:
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>祝你成功
- 已标记为答案 微软中文技术论坛Moderator 2009年6月30日 5:27
-
添加
clientaccesspolicy.xml
也可以
<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>- 已标记为答案 微软中文技术论坛Moderator 2009年6月30日 5:27
-
如果你打算在Silverlight 2 Beta 2 里面访问 Web 服务,需要使用一个ClientAccessPolicy.xml文件,此外,你的URL不能含有下划线,如 Beta1 中的 MyApp_Web等,只需将工程文件重新命名即可。
这里还有一个提示,如果服务器上已经部署了Flash的访问策略文件,将直接被Silverlight支持,不用重复部署了。
<?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>