VS2003调用java下的webservice(rpc-literal)
公司有个项目基于古老的MC1000 PDA设备,该款PDA上自带的.NET Compact Framework为1.0,项目组决定用VS2003来开发其上的应用程序。
遇到一个问题:PDA应用程序需要通过一个java编写的WebService与服务器端进行数据交互,但添加Web引用后,编译时会提示“不支持 style=rpc 和 use=literal 的组合”,而实际的调用方法也没有生成;但用VS2005测试没有任何问题。
网上查到的说法是:VS2003支持document/literal和document/encoded,而rpc/XX未实现。有人提示说可以通过HttpWebRequest方式直接调用,但始终没能找到完整代码。
通过拼凑和尝试,以下为可用的代码:
private string WSBaseDataSyn(string userNo, string synTime) { string strURL = "http://10.194.129.197:8889/pdas/services/pdasws"; string result = string.Empty; try { //创建一个HTTP请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); //Post请求方式 request.Method = "POST"; //内容类型 request.ContentType = "text/xml"; string strXMLRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>" + "<WSBaseDataSyn>" + "<in0>{0}</in0>" + "<in1>{1}</in1>" + "</WSBaseDataSyn>" + "</soap:Body>" + "</soap:Envelope>"; strXMLRequest = string.Format(strXMLRequest, userNo, synTime); XmlDocument soapEnvelopeXml = new XmlDocument(); soapEnvelopeXml.LoadXml(strXMLRequest); //发送请求,获得请求流 using (Stream writer = request.GetRequestStream()) { //将请求参数写入流 soapEnvelopeXml.Save(writer); } //获得响应流 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream s = response.GetResponseStream()) { //转化为XML,自己进行处理 //using (XmlTextReader reader = new XmlTextReader(s)) using (StreamReader reader = new StreamReader(s)) { //reader.MoveToContent(); //string strValue = reader.ReadInnerXml(); string strValue = reader.ReadToEnd(); XmlDocument responseXml = new XmlDocument(); responseXml.LoadXml(strValue); if (responseXml.HasChildNodes) { XmlNode node = responseXml.FirstChild; while (node.HasChildNodes) { node = node.FirstChild; } //out的实际结果 return node.InnerText; } } } } catch (Exception ex) { result = ex.Message; } return result; }
需要注意的是这段代码:
"<WSBaseDataSyn>" + "<in0>{0}</in0>" + "<in1>{1}</in1>" + "</WSBaseDataSyn>" +