CF.NET 2.0 通过cmwap访问外部webService

2023-10-08 15:40

本文主要是介绍CF.NET 2.0 通过cmwap访问外部webService,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个帖子本16号放在首页,不知道为何不见了,也许是我误删除了吧,重新补上,放在我自己的首页。

最近在做手机上一程序,需要通过移动cmwap访问外部的webservice,但使用wsdl生成的代理类通过cmwap网关访问会出现"Client found response content type of 'text/vnd.wap.wml',but expected 'text/xml' ".的异常信息,换用cmnet网络则正常。
应该是通过cmwap网关请求返回的数据被转化为wml类型了,wsdl代理类只认xml;
把google翻了也找不到解决方案,只好自己用最笨的办法,用HttpWebRequest和HttpWebResponse实现访问soap协议。

代码如下:

ContractedBlock.gif ExpandedBlockStart.gif SoapClient
ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gifcode by Aijoe(原 Ivan Chin) 
InBlock.gif
http://www.lostway.net/
InBlock.gif
http://www.cnblogs.com/drw/
InBlock.gifaijoe@lostway.net
InBlock.gif转载请保留此信息。
ExpandedBlockEnd.gif
*/

None.gif
None.gif
using System;
None.gif
using System.Collections;
None.gif
using System.Collections.Generic;
None.gif
using System.IO;
None.gif
using System.Net;
None.gif
using System.Text;
None.gif
using System.Threading;
None.gif
using System.Xml;
None.gif
None.gif
namespace IvanCF.Soap
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
internal class SoapClient 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
InBlock.gif
InBlock.gif        
private string nameSpace = "http://tempurl.org/";
InBlock.gif        
private Uri serviceUrl;
InBlock.gif        
private IWebProxy webProxy = null;
InBlock.gif        
private ICredentials credentials = null;
InBlock.gif        
private int timeOut = 30000;
InBlock.gif        
private Encoding encoding = Encoding.GetEncoding("UTF-8");
InBlock.gif
InBlock.gif        
//设置请求的webservice命名空间
InBlock.gif
        public string Namespace
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn nameSpace; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ nameSpace = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//webservice地址
InBlock.gif
        public Uri ServiceUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn serviceUrl; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ serviceUrl = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//客户端使用代理
InBlock.gif
        public IWebProxy Proxy
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn webProxy; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ webProxy = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//客户端验证
InBlock.gif
        public ICredentials Credentials
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn credentials; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ credentials = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//webservice请求编码格式
InBlock.gif
        public Encoding ContentEncoding
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn encoding; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ encoding = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//超时时间
InBlock.gif
        public int TimeOut
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn timeOut; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ timeOut = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
InBlock.gif        
private const int BUFFER_SIZE = 512;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
异步方法#region 异步方法
InBlock.gif
InBlock.gif        
//用于操作等待线程。
InBlock.gif
        public static ManualResetEvent allDone = new ManualResetEvent(false);
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
实现事件完成通知#region 实现事件完成通知
InBlock.gif
InBlock.gif        
//异步调用完成的事件
InBlock.gif
        public event SoapInvokeCompletedEventHandler SoapInvokeCompleted;
InBlock.gif        
protected virtual void OnSoapInvokeCompleted(SoapInvokeCompleteArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (SoapInvokeCompleted != null)
InBlock.gif                SoapInvokeCompleted(
this, args);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 异步调用webService的方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="methodName">调用的webService的方法名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="parameters">发送的数据</param>

InBlock.gif        public void InvokeAsync(string methodName, PostDataArray parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RequestState state 
= new RequestState();
InBlock.gif            state.MethodName 
= methodName;
InBlock.gif            state.Parameters 
= parameters;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                state.Request 
= (HttpWebRequest) WebRequest.Create(ServiceUrl);
InBlock.gif                
if (TimeOut > 0) state.Request.Timeout = TimeOut;
InBlock.gif                
if (Proxy != null) state.Request.Proxy = Proxy;
InBlock.gif                
if (Credentials != null) state.Request.Credentials = Credentials;
InBlock.gif                state.DataSend.Append(ResloveSendData(methodName, parameters));
InBlock.gif                state.Request.ContentLength 
= ContentEncoding.GetByteCount(state.DataSend.ToString());
InBlock.gif                state.Request.Method 
= "POST";
InBlock.gif                state.Request.ContentType 
= "application/soap+xml; charset=" + ContentEncoding.WebName.ToLower();
InBlock.gif
InBlock.gif                
//开始获取请求流
InBlock.gif
                state.Request.BeginGetRequestStream(new AsyncCallback(ReqCall), state);
InBlock.gif
InBlock.gif                
//阻塞当前进程,直到收到完成的通知。
InBlock.gif
                allDone.WaitOne();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                state.Error 
= ex;
InBlock.gif                allDone.Set();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                allDone.Reset();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string rData = null;
InBlock.gif            
if (state.Error == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlDocument doc 
= new XmlDocument();
InBlock.gif                doc.LoadXml(state.DataRecived.ToString());
InBlock.gif                rData 
= ResloveRecivedData(methodName, doc);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string sData = state.DataSend.ToString();
InBlock.gif            SoapInvokeCompleteArgs args 
= new SoapInvokeCompleteArgs(methodName,
InBlock.gif                                                                     sData,
InBlock.gif                                                                     rData,
InBlock.gif                                                                     state.Error);
InBlock.gif            
//访问完成通知Soap完成事件。
InBlock.gif
            OnSoapInvokeCompleted(args);
InBlock.gif            state.Close();
InBlock.gif            GC.WaitForPendingFinalizers();
InBlock.gif            GC.Collect();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ReqCall(IAsyncResult ar)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RequestState state 
= (RequestState) ar.AsyncState;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//写入请求
InBlock.gif
                StreamWriter streamWriter = new StreamWriter(state.Request.EndGetRequestStream(ar));
InBlock.gif                streamWriter.Write(state.DataSend.ToString());
InBlock.gif                streamWriter.Flush();
InBlock.gif                streamWriter.Close();
InBlock.gif
InBlock.gif                
//开始取得响应。
InBlock.gif
                state.Request.BeginGetResponse(new AsyncCallback(ResCall), state);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                state.Error 
= ex;
InBlock.gif                allDone.Set();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ResCall(IAsyncResult ar)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RequestState state 
= (RequestState) ar.AsyncState;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//取响应完毕。
InBlock.gif
                state.Response = (HttpWebResponse) state.Request.EndGetResponse(ar);
InBlock.gif                state.ResponseStream 
= state.Response.GetResponseStream();
InBlock.gif                
//开始读取流。
InBlock.gif
                state.ResponseStream.BeginRead(state.BufferRead, 0, BUFFER_SIZE,
InBlock.gif                                               
new AsyncCallback(ReadCall), state);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                state.Error 
= ex;
InBlock.gif                allDone.Set();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ReadCall(IAsyncResult ar)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RequestState state 
= (RequestState) ar.AsyncState;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//分段读取流。
InBlock.gif
                int read = state.ResponseStream.EndRead(ar);
InBlock.gif                
if (read > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string strRes = ContentEncoding.GetString(state.BufferRead, 0, read);
InBlock.gif                    state.DataRecived.Append(strRes);
InBlock.gif                    state.ResponseStream.BeginRead(state.BufferRead, 
0, BUFFER_SIZE,
InBlock.gif                                                   
new AsyncCallback(ReadCall), state);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    allDone.Set();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                state.Error 
= ex;
InBlock.gif                allDone.Set();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
同步方法#region 同步方法 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 同步调用webService的方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="methodName">方法名称</param>
InBlock.gif        
/// <param name="data">发送的xml数据</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回的xml数据</returns>

InBlock.gif        public InvokeResult Invoke(string methodName, PostDataArray data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InvokeResult result 
= new InvokeResult();
InBlock.gif            result.MethodName 
= methodName;
InBlock.gif            HttpWebRequest request 
= (HttpWebRequest) WebRequest.Create(ServiceUrl);
InBlock.gif
InBlock.gif            
if (TimeOut > 0) request.Timeout = TimeOut;
InBlock.gif            
if (Proxy != null) request.Proxy = Proxy;
InBlock.gif            
if (Credentials != null) request.Credentials = Credentials;
InBlock.gif
InBlock.gif            result.DataSend 
= ResloveSendData(methodName, data);
InBlock.gif            request.ContentLength 
= ContentEncoding.GetByteCount(result.DataSend);
InBlock.gif            request.Method 
= "POST";
InBlock.gif            request.ContentType 
= "application/soap+xml; charset=" + ContentEncoding.WebName.ToLower();
InBlock.gif
InBlock.gif            StreamWriter streamWriter 
= new StreamWriter(request.GetRequestStream());
InBlock.gif            streamWriter.Write(result.DataSend);
InBlock.gif            streamWriter.Flush();
InBlock.gif            streamWriter.Close();
InBlock.gif
InBlock.gif            HttpWebResponse response 
= null;
InBlock.gif            Stream responseStream 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                response 
= (HttpWebResponse) request.GetResponse();
InBlock.gif                responseStream 
= response.GetResponseStream();
InBlock.gif                XmlDocument doc 
= new XmlDocument();
InBlock.gif                doc.Load(responseStream);
InBlock.gif                result.DataRecived 
= ResloveRecivedData(methodName, doc);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result.Error 
= ex;
InBlock.gif                
//AppNet.GetLog().wLog(ex.ToString(), "Exception at " + invokeMethod + " Invoke");
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (response != null) response.Close();
InBlock.gif                
if (responseStream != null) responseStream.Close();
InBlock.gif                GC.WaitForPendingFinalizers();
InBlock.gif                GC.Collect();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
helper Method#region helper Method 
InBlock.gif
InBlock.gif        
//将发送的数据从PostDataArray转化成soap1.2的xml数据格式。
InBlock.gif
        private string ResloveSendData(string invokeMethod, PostDataArray data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string soapNs = "http://www.w3.org/2003/05/soap-envelope";
InBlock.gif            
string strSoapData;
InBlock.gif            
using (MemoryStream stream = new MemoryStream())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (XmlTextWriter writer = new XmlTextWriter(stream, ContentEncoding))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    writer.WriteStartDocument();
InBlock.gif                    writer.WriteStartElement(
"soap12""Envelope", soapNs);
InBlock.gif                    writer.WriteStartElement(
"soap12""Body", soapNs);
InBlock.gif                    writer.WriteStartElement(invokeMethod, Namespace);
InBlock.gif                    AddData2XmlWriter(writer, data);
InBlock.gif                    writer.WriteEndElement();
InBlock.gif                    writer.WriteEndElement();
InBlock.gif                    writer.WriteEndElement();
InBlock.gif                    writer.Flush();
InBlock.gif                    stream.Seek(
0, SeekOrigin.Begin);
InBlock.gif                    
using (StreamReader reader = new StreamReader(stream))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        strSoapData 
= reader.ReadToEnd();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strSoapData;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//我对返回的xml数据进行了简单的处理,仅留下了<方法名Result></方法名Result>这一节,以便后续处理。
InBlock.gif
        private string ResloveRecivedData(string invokeMethod, XmlDocument doc)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool IsSoap12 = false;
InBlock.gif            
if (doc.OuterXml.IndexOf("soap12:Envelope"!= -1) IsSoap12 = true;
InBlock.gif            XmlNamespaceManager nsmanager 
= new XmlNamespaceManager(doc.NameTable);
InBlock.gif            
if (IsSoap12)
InBlock.gif                nsmanager.AddNamespace(
"soap12""http://www.w3.org/2003/05/soap-envelope");
InBlock.gif            
else
InBlock.gif                nsmanager.AddNamespace(
"soap""http://www.w3.org/2003/05/soap-envelope");
InBlock.gif            
string xpath = "//soap:Envelope/soap:Body";
InBlock.gif            
if (IsSoap12) xpath = "//soap12:Envelope/soap12:Body";
InBlock.gif            XmlNode node 
= doc.SelectSingleNode(xpath, nsmanager);
InBlock.gif            
string strDoc = node.InnerXml;
InBlock.gif            strDoc 
=
InBlock.gif                strDoc.Replace(
"<" + invokeMethod + "Response xmlns=\"" + Namespace + "\"",
InBlock.gif                               
"<" + invokeMethod + "Response");
InBlock.gif            doc 
= new XmlDocument();
InBlock.gif            doc.LoadXml(strDoc);
InBlock.gif            XmlNode rNode 
= doc.SelectSingleNode(invokeMethod + "Response");
InBlock.gif            strDoc 
= rNode.InnerXml;
InBlock.gif            
return strDoc;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//添加PostDataArray到XmlWriter(这个方法为ResloveSendData服务)
InBlock.gif
        private void AddData2XmlWriter(XmlWriter writer, PostDataArray datas)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (datas.ColName.Length > 0) writer.WriteStartElement(datas.ColName);
InBlock.gif            
foreach (PostData d in datas)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                writer.WriteStartElement(d.Key);
InBlock.gif                
if (d.Value.GetType() == typeof (byte[]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
byte[] bt = (byte[]) d.Value;
InBlock.gif                    writer.WriteBase64(bt, 
0, bt.Length);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
InBlock.gif                    writer.WriteValue(d.Value);
InBlock.gif                writer.WriteEndElement();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (datas.ColName.Length > 0) writer.WriteEndElement();
InBlock.gif
InBlock.gif            
if (datas.Childs.Count > 0)
InBlock.gif                
foreach (PostDataArray col in datas.Childs)
InBlock.gif                    AddData2XmlWriter(writer, col);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
这里是几个辅助类,自己去看看吧#region 这里是几个辅助类,自己去看看吧
InBlock.gif
InBlock.gif    
//存储同步调用后的结果
InBlock.gif
    internal struct InvokeResult
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Exception Error;
InBlock.gif        
public string DataRecived;
InBlock.gif        
public string DataSend;
InBlock.gif        
public string MethodName;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
internal struct PostData
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public PostData(string key, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Key 
= key;
InBlock.gif            Value 
= value;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Key;
InBlock.gif        
public object Value;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
internal class PostDataArray : CollectionBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public PostDataArray()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dataCols 
= new List<PostDataArray>();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private List<PostDataArray> dataCols;
InBlock.gif        
private string colN = "";
InBlock.gif
InBlock.gif        
public string ColName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn colN; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ colN = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public PostData this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (PostData) List[index]; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public List<PostDataArray> Childs
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn dataCols; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Add(PostData obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List.Add(obj);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Add(string key, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List.Add(
new PostData(key, value));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Add(PostDataArray obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dataCols.Add(obj);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public new void Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.Clear();
InBlock.gif            dataCols.Clear();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
internal class RequestState
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public const int BUFFER_SIZE = 512;
InBlock.gif        
public byte[] BufferRead;
InBlock.gif        
public HttpWebRequest Request;
InBlock.gif        
public HttpWebResponse Response;
InBlock.gif        
public Stream ResponseStream;
InBlock.gif        
public Stream RequestStream;
InBlock.gif        
public string MethodName;
InBlock.gif        
public PostDataArray Parameters;
InBlock.gif        
public StringBuilder DataSend;
InBlock.gif        
public StringBuilder DataRecived;
InBlock.gif        
public Exception Error;
InBlock.gif
InBlock.gif        
public RequestState()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            BufferRead 
= new byte[BUFFER_SIZE];
InBlock.gif            DataRecived 
= new StringBuilder();
InBlock.gif            DataSend 
= new StringBuilder();
InBlock.gif            Request 
= null;
InBlock.gif            ResponseStream 
= null;
InBlock.gif            RequestStream 
= null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Close()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (ResponseStream != null) ResponseStream.Close();
InBlock.gif            
if (Response != null) Response.Close();
InBlock.gif            
if (Request != null) Request.Abort();
InBlock.gif            Response 
= null;
InBlock.gif            Request 
= null;
InBlock.gif            ResponseStream 
= null;
InBlock.gif            RequestStream 
= null;
InBlock.gif            DataRecived 
= null;
InBlock.gif            DataSend 
= null;
InBlock.gif            MethodName 
= null;
InBlock.gif            Parameters 
= null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
InBlock.gif    
//储存事件通知结果
InBlock.gif
    internal struct SoapInvokeCompleteArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public SoapInvokeCompleteArgs(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MethodName 
= null;
InBlock.gif            Error 
= ex;
InBlock.gif            DataSend 
= null;
InBlock.gif            DataRecived 
= null;
InBlock.gif            IsCompleted 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SoapInvokeCompleteArgs(string data, string methodname)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MethodName 
= methodname;
InBlock.gif            Error 
= null;
InBlock.gif            DataSend 
= null;
InBlock.gif            DataRecived 
= data;
InBlock.gif            IsCompleted 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SoapInvokeCompleteArgs(string methodname, string sendData, string recivedData, Exception error)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MethodName 
= methodname;
InBlock.gif            DataSend 
= sendData;
InBlock.gif            DataRecived 
= recivedData;
InBlock.gif            Error 
= error;
InBlock.gif            IsCompleted 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//调用的方法名称
InBlock.gif
        public string MethodName;
InBlock.gif        
//返回的xml数据
InBlock.gif
        public string DataRecived;
InBlock.gif        
//发送给weService的xml数据
InBlock.gif
        public string DataSend;
InBlock.gif        
//调用过程的错误信息
InBlock.gif
        public Exception Error;
InBlock.gif        
//是否已完成
InBlock.gif
        public bool IsCompleted;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//委托完成事件
InBlock.gif
    internal delegate void SoapInvokeCompletedEventHandler(object sender, SoapInvokeCompleteArgs e);
InBlock.gif
ExpandedBlockEnd.gif}
代码说明:
代码原理比较简单,主要是了解Soap1.2的格式和xml操作。
已经在代码中加入了部分注释。下面是一个调用例子
void getwebservice(){PostDataArray data = new PostDataArray();data.Add("参数1", "参数值1");PostDataArray indata = new PostDataArray();indata.Add("嵌套参数1","嵌套参数值1");indata.ColName = "嵌套节点名称";data.Add(indata);SoapClient soap = new SoapClient();soap.ContentEncoding = Encoding.GetEncoding(65001);soap.Proxy = new WebProxy("代理");soap.ServiceUrl = new Uri("webService地址");soap.TimeOut = 180000;//超时时间(毫秒)//用同步方法调用ws,直接返回InvokeResult结果;soap.Invoke("webService方法名称", data);//用异步方法调用ws,在SoapInvokeCompleted事件中处理结果。soap.SoapInvokeCompleted += new SoapInvokeCompletedEventHandler(soap_SoapInvokeCompleted);soap.InvokeAsync("webService方法名称", data);
}

转载于:https://www.cnblogs.com/drw/archive/2007/02/21/cf_net_cmwap_webservice.html

这篇关于CF.NET 2.0 通过cmwap访问外部webService的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/166324

相关文章

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

Java访问修饰符public、private、protected及默认访问权限详解

《Java访问修饰符public、private、protected及默认访问权限详解》:本文主要介绍Java访问修饰符public、private、protected及默认访问权限的相关资料,每... 目录前言1. public 访问修饰符特点:示例:适用场景:2. private 访问修饰符特点:示例:

Idea调用WebService的关键步骤和注意事项

《Idea调用WebService的关键步骤和注意事项》:本文主要介绍如何在Idea中调用WebService,包括理解WebService的基本概念、获取WSDL文件、阅读和理解WSDL文件、选... 目录前言一、理解WebService的基本概念二、获取WSDL文件三、阅读和理解WSDL文件四、选择对接

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

使用Python实现批量访问URL并解析XML响应功能

《使用Python实现批量访问URL并解析XML响应功能》在现代Web开发和数据抓取中,批量访问URL并解析响应内容是一个常见的需求,本文将详细介绍如何使用Python实现批量访问URL并解析XML响... 目录引言1. 背景与需求2. 工具方法实现2.1 单URL访问与解析代码实现代码说明2.2 示例调用

关于Java内存访问重排序的研究

《关于Java内存访问重排序的研究》文章主要介绍了重排序现象及其在多线程编程中的影响,包括内存可见性问题和Java内存模型中对重排序的规则... 目录什么是重排序重排序图解重排序实验as-if-serial语义内存访问重排序与内存可见性内存访问重排序与Java内存模型重排序示意表内存屏障内存屏障示意表Int

SpringBoot实现基于URL和IP的访问频率限制

《SpringBoot实现基于URL和IP的访问频率限制》在现代Web应用中,接口被恶意刷新或暴力请求是一种常见的攻击手段,为了保护系统资源,需要对接口的访问频率进行限制,下面我们就来看看如何使用... 目录1. 引言2. 项目依赖3. 配置 Redis4. 创建拦截器5. 注册拦截器6. 创建控制器8.

SpringBoot如何访问jsp页面

《SpringBoot如何访问jsp页面》本文介绍了如何在SpringBoot项目中进行Web开发,包括创建项目、配置文件、添加依赖、控制层修改、测试效果以及在IDEA中进行配置的详细步骤... 目录SpringBoot如何访问JSP页python面简介实现步骤1. 首先创建的项目一定要是web项目2. 在

Linux限制ip访问的解决方案

《Linux限制ip访问的解决方案》为了修复安全扫描中发现的漏洞,我们需要对某些服务设置访问限制,具体来说,就是要确保只有指定的内部IP地址能够访问这些服务,所以本文给大家介绍了Linux限制ip访问... 目录背景:解决方案:使用Firewalld防火墙规则验证方法深度了解防火墙逻辑应用场景与扩展背景:

Python使用pysmb库访问Windows共享文件夹的详细教程

《Python使用pysmb库访问Windows共享文件夹的详细教程》本教程旨在帮助您使用pysmb库,通过SMB(ServerMessageBlock)协议,轻松连接到Windows共享文件夹,并列... 目录前置条件步骤一:导入必要的模块步骤二:配置连接参数步骤三:实例化SMB连接对象并尝试连接步骤四: