Silverlight杂记-获取网络数据源的几种方式
2010-12-25 01:33 撞破南墙 阅读(2072) 评论(0) 编辑 收藏 举报 1跨域验证
<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>
2与数据源连接
1 SOAP services
服务端
新建一个asmx 得到
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class SampleAsmx : System.Web.Services.WebService
{
[WebMethod]
public string GetCoolText(int number)
{
switch (number)
{
case 0: return "zero";
case 1: return "one";
default: return "unknown";
}
}
客户端
Binding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new
EndpointAddress("http://localhost:55905/SampleAsmx.asmx");
SilverService.SampleAsmxSoapClient proxy = new
SilverService.SampleAsmxSoapClient(myBinding, myEndpoint);
proxy.GetCoolTextCompleted += proxy_GetCoolTextCompleted;
proxy.GetCoolTextAsync(1);
}
void proxy_GetCoolTextCompleted(object sender,
SilverService.GetCoolTextCompletedEventArgs e) {
Results.Text = e.Result;
(sender as SilverService.SampleAsmxSoapClient).CloseAsync();
}
客户端的config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SampleAsmxSoap" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:55905/SampleAsmx.asmx" binding="basicHttpBinding"
bindingConfiguration="SampleAsmxSoap" contract="SilverService.SampleAsmxSoap"
name="SampleAsmxSoap" />
</client>
</system.serviceModel>
</configuration>
这里会涉及到一堆WCF 的知识。可能需要你额外去学习。
2 RESTful Sercvices
从RESTFUL的services上获取信息
{
UIThread = SynchronizationContext.Current;
string rawPath
= "http://www.silverlightinaction.com/Authors.svc/SingleXml/{0}";
Uri path = new Uri(string.Format(rawPath, Input.Text),
UriKind.Absolute);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
request.BeginGetResponse(SingleXmlCallBack, request);
}
private void SingleXmlCallBack(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response
= (HttpWebResponse)request.EndGetResponse(result);
Stream responseStream = response.GetResponseStream();
UIThread.Post(UpdateUiText, responseStream);
}
发送信息过去
private void Test_Click(object sender, RoutedEventArgs e)
{
UIThread = SynchronizationContext.Current;
Uri path = new
Uri("http://www.silverlightinaction.com/Authors.svc/Update/Brown",
UriKind.Absolute);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
request.Method = "POST";
request.ContentType = "application/xml";
request.BeginGetRequestStream(AddPayload, request);
}
private void AddPayload(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest) result.AsyncState;
StreamWriter dataWriter =
new StreamWriter(request.EndGetRequestStream(result));
dataWriter.Write("<?xml version=\"1.0\"?><Author><FirstName>Bob" +
"</FirstName><LastName>Smith</LastName></Author>");
dataWriter.Close();
request.BeginGetResponse(SingleJsonCallBack, request);
}
private void SingleJsonCallBack(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response =
(HttpWebResponse)request.EndGetResponse(result);
Stream responseStream = response.GetResponseStream();
UIThread.Post(UpdateUiText, responseStream);
}
3HTTP方式
1手动构造
使用System.Net.Browser.WebRequestCreator 来构造。
HttpWebRequest request =
(HttpWebRequest)WebRequestCreator.ClientHttp.Create(
new Uri("http://api.10rem.net/Authors"));
request.Method = "PUT";
2为特定的网站或协议自动使用client stack
如为某个网站 HTTP 和 SSL都使用 client stack 你可以这么写
为指定的 URI 注册 WebRequest 子代。
http://www.baidu.com, WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix(
"https://www.baidu.com", WebRequestCreator.ClientHttp);
3Authentication credentials
The client networking stack supports NTLM, basic, and digest authorization
NTLM是NT LAN Manager的缩写,这也说明了协议的来源。NTLM 是 Windows NT 早期版本的标准安全协议,Windows 2000 支持 NTLM 是为了保持向后兼容。Windows 2000内置三种基本安全协议之一。
在网络环境中,NTLM 用作身份验证协议以处理两台计算机(其中至少有一台计算机运行 Windows NT 4.0 或更早版本)之间的事务。具有此配置的网络称为“混合模式”,这是 Windows Server 2003 家族中的默认设置
参考
http://www.codeproject.com/KB/aspnet/ASPDOTNETauthentication.aspx
实现 digest authorization
{
HttpWebRequest.RegisterPrefix(
"http://", WebRequestCreator.ClientHttp);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(
new Uri("http://XX.net"));
req.UseDefaultCredentials = false;
req.Credentials =
new NetworkCredential("Pete", "password");//任意多参数
req.BeginGetResponse(OnRequestCompleted, req);
}
private void OnRequestCompleted(IAsyncResult asyncResult)
{
HttpWebRequest request =
(HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response =
(HttpWebResponse)request.EndGetResponse(asyncResult);
................
}
4使用cookies
{
WebRequest.RegisterPrefix(
"http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix(
"https://", WebRequestCreator.ClientHttp);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(
new Uri("http://baidu.com"));
CookieCollection cookies = new CookieCollection();
cookies.Add(new Cookie("firstName", "Pete"));
cookies.Add(new Cookie("lastName", "Brown"));
cookies.Add(new Cookie("lastAccess", DateTime.Now.ToString()));
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(
new Uri("http://baidu.com"), cookies);
req.BeginGetResponse(OnRequestCompleted, req);
}
private void OnRequestCompleted(IAsyncResult asyncResult)
{
HttpWebRequest req =
(HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response =
(HttpWebResponse)req.EndGetResponse(asyncResult);
...
foreach (Cookie cookie in response.Cookies)
{
Debug.WriteLine(cookie.Name + ":" + cookie.Value);
}
}
作者:撞破南墙
出处:http://www.cnblogs.com/facingwaller/
关于作者:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。