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

相关文章

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Java -jar命令如何运行外部依赖JAR包

《Java-jar命令如何运行外部依赖JAR包》在Java应用部署中,java-jar命令是启动可执行JAR包的标准方式,但当应用需要依赖外部JAR文件时,直接使用java-jar会面临类加载困... 目录引言:外部依赖JAR的必要性一、问题本质:类加载机制的限制1. Java -jar的默认行为2. 类加

如何搭建并配置HTTPD文件服务及访问权限控制

《如何搭建并配置HTTPD文件服务及访问权限控制》:本文主要介绍如何搭建并配置HTTPD文件服务及访问权限控制的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、安装HTTPD服务二、HTTPD服务目录结构三、配置修改四、服务启动五、基于用户访问权限控制六、

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

NGINX 配置内网访问的实现步骤

《NGINX配置内网访问的实现步骤》本文主要介绍了NGINX配置内网访问的实现步骤,Nginx的geo模块限制域名访问权限,仅允许内网/办公室IP访问,具有一定的参考价值,感兴趣的可以了解一下... 目录需求1. geo 模块配置2. 访问控制判断3. 错误页面配置4. 一个完整的配置参考文档需求我们有一

C#实现访问远程硬盘的图文教程

《C#实现访问远程硬盘的图文教程》在现实场景中,我们经常用到远程桌面功能,而在某些场景下,我们需要使用类似的远程硬盘功能,这样能非常方便地操作对方电脑磁盘的目录、以及传送文件,这次我们将给出一个完整的... 目录引言一. 远程硬盘功能展示二. 远程硬盘代码实现1. 底层业务通信实现2. UI 实现三. De

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

Nginx 访问 /root/下 403 Forbidden问题解决

《Nginx访问/root/下403Forbidden问题解决》在使用Nginx作为Web服务器时,可能会遇到403Forbidden错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录解决 Nginx 访问 /root/test/1.html 403 Forbidden 问题问题复现Ng

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr