关于SilverLight 使用HttpWebRequest请求的问题
void Demo()
{
HttpWebRequest request = WebRequest.Create(
new Uri("http://localhost:8080/web/Search?query=tiger", UriKind.Absolute)) as HttpWebRequest;
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
}
private void ResponseCallback(IAsyncResult result)
{
// IAsyncResult.AsyncState - AsyncCallback 传过来的对象
HttpWebRequest request = result.AsyncState as HttpWebRequest;
WebResponse response = null;
try
{
response = request.EndGetResponse(result) as HttpWebResponse;
if (response != null)
{
Stream responseStream = response.GetResponseStream();
using (StreamReader sr = new StreamReader(responseStream))
{
string s = sr.ReadToEnd();
}
}
}
catch (Exception ex)
{
}
}
每次response = request.EndGetResponse(result) as HttpWebResponse;
都会抛Security Exception.
调用的是Java Servlet. --------------------编程问答-------------------- 这个可能是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>
--------------------编程问答-------------------- 可能是跨域问题,添加跨域文件clientaccesspolicy.xml
还有你的地址
http://localhost:8080/web/Search?query=tiger
将localhost换成IP地址试试
补充:.NET技术 , Web Services