Wince 中访问WCF服务
2020-12-07 01:34
标签:des style blog class code java
由于本文并非WinCE开发普及篇,所以一些WinCE开发和WCF开发的基础还请移步百度和谷歌寻找答案,然后结合本文开发出WinCE中如何访问WCF,谢谢。
开发环境
IDE:Visual Studio 2008 (2010、2012、2013目前都不支持)
OS:Win 7 (64位)
Tools:ActiveSync win7 v6.1(设备中心,给Pocket PC 2003模拟器提供网络)
模拟器网络连接攻略一份:http://www.jb51.net/softjc/42088.html
创建WinCE项目
请恕本文并非WinCE开发普及篇,所以这些请百度吧。
WCF服务端
app.config中关键代码
View Code"SystemDispatchServiceForPDABehavior" name="SystemManageServiceLibrary.SystemDispatchServiceForPDA"> "http://localhost:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA" binding="webHttpBinding" contract="SystemManageServiceLibrary.SystemDispatch.ISystemDispatchServiceForPDA" > "http://localhost:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA/mex" binding="mexHttpBinding" contract="IMetadataExchange" /> "http://localhost:20003/SystemDispatchForPDA"/> "00:00:30" /> 服务契约 - 公布WCF REST(详细的可以百度搜索 WCF REST)
View Code[ServiceContract] public interface ISystemDispatchServiceForPDA { ////// PDA获取集群信息 /// /// PDA内部出厂序号 /// [OperationContract] //UriTemplate 实际就是通过http协议发送请求的url规则,把{strPDA_IMEI}替换成真实的PDA串号即可 [WebGet(UriTemplate = "GetClusterInfo/{strPDA_IMEI}")] CLUSTER GetClusterInfo(string strPDA_IMEI); }
WinCE
HttpWrapper.cs - Http请求的封装,访问WCF提供的REST服务
View Codepublic class HttpWrapper { public static string SendRequest(string url) { HttpWebResponse response = null; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.AllowWriteStreamBuffering = false; request.KeepAlive = true; request.ContentType = "application/x-www-form-urlencoded"; // 接收返回的页面 response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); string strResult = reader.ReadToEnd(); reader.Close(); response.Close(); return strResult; } }XmlAdapter.cs - Xml适配器,用于将Xml转换成类
View Codepublic class XmlAdapter { public static T ConvertToClass(string strXML) where T : class { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); MemoryStream reader = new MemoryStream(Encoding.UTF8.GetBytes(strXML)); T obj = xmlSerializer.Deserialize(reader) as T; reader.Dispose(); return obj; } } 调用方法
private static string URL = "http://ip:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA/"; public static CLUSTER GetClusterInfo(string strPDA_IMEI) { string strResponse = HttpWrapper.SendRequest(URL + "GetClusterInfo/" + strPDA_IMEI); CLUSTER cluster = XmlAdapter.ConvertToClass(strResponse); return cluster; }
真正需要注意的其实就是几点:
1.安装设备中心
2.设置模拟器网络连接
3.WCF REST
4.WinCE解析WCF返回的XML,以及如何拼接访问的URL
Wince 中访问WCF服务,搜素材,soscw.com
Wince 中访问WCF服务
标签:des style blog class code java
原文地址:http://www.cnblogs.com/doddgu/p/3718778.html