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

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

Javascript访问Promise对象返回值的操作方法

《Javascript访问Promise对象返回值的操作方法》这篇文章介绍了如何在JavaScript中使用Promise对象来处理异步操作,通过使用fetch()方法和Promise对象,我们可以从... 目录在Javascript中,什么是Promise1- then() 链式操作2- 在之后的代码中使

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

本地搭建DeepSeek-R1、WebUI的完整过程及访问

《本地搭建DeepSeek-R1、WebUI的完整过程及访问》:本文主要介绍本地搭建DeepSeek-R1、WebUI的完整过程及访问的相关资料,DeepSeek-R1是一个开源的人工智能平台,主... 目录背景       搭建准备基础概念搭建过程访问对话测试总结背景       最近几年,人工智能技术

Ollama整合open-webui的步骤及访问

《Ollama整合open-webui的步骤及访问》:本文主要介绍如何通过源码方式安装OpenWebUI,并详细说明了安装步骤、环境要求以及第一次使用时的账号注册和模型选择过程,需要的朋友可以参考... 目录安装环境要求步骤访问选择PjrIUE模型开始对话总结 安装官方安装地址:https://docs.

解读静态资源访问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 访问修饰符特点:示例: