利用WSDL文档在.NET平台下的WebService调用的例子

None.gif HttpWebRequest httpRequest;
None.gif            
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif            
InBlock.gif                httpRequest
=(HttpWebRequest)WebRequest.Create(this.cbox_url.Text);
InBlock.gif
ExpandedBlockEnd.gif            }

None.gif            
catch (SystemException se)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                MessageBox.Show(
this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
InBlock.gif                
return;
ExpandedBlockEnd.gif            }

None.gif            httpRequest.Method
= " GET " ;
None.gif            HttpWebResponse httpResponse;
None.gif            
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif            
InBlock.gif                httpResponse
=(HttpWebResponse)httpRequest.GetResponse();
ExpandedBlockEnd.gif            }

None.gif            
catch (SystemException se)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {    
InBlock.gif                MessageBox.Show(
this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
InBlock.gif                
return;
ExpandedBlockEnd.gif            }

None.gif            ServiceDescription wsdl;
None.gif            
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                wsdl
=ServiceDescription.Read(httpResponse.GetResponseStream());
ExpandedBlockEnd.gif            }

None.gif            
catch (SystemException se)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                MessageBox.Show(
this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
InBlock.gif                
return;
ExpandedBlockEnd.gif            }

None.gif            
finally
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                httpResponse.Close();
InBlock.gif
ExpandedBlockEnd.gif            }

None.gif            
// First Step:  View "Service" 元素,第一步,检查service元素
None.gif
             int  servicesCount = wsdl.Services.Count;
None.gif            ServicesDescription[] sd
= new  ServicesDescription[servicesCount];  // 这个类是用来封装所有的信息的
None.gif
            FunctionDescription fd = null ;
None.gif            Service service
= null ;
None.gif            
// Port portGet=null; // 这里的端口就是协议,httpGet,httpPost,soap
None.gif
            
None.gif            
None.gif            
if  ( this .cbox_ReqType.SelectedItem.ToString() == " Soap "
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
this.ActionType=SOAP;
InBlock.gif                
this.AddressBindingString="SoapAddressBinding";
InBlock.gif                
for(int i=0;i<servicesCount;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    service
=wsdl.Services[i];
InBlock.gif                    sd[i]
=new ServicesDescription();
InBlock.gif                    sd[i].ServiceName
=service.Name;
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
for(int j=0;j<service.Ports[service.Name+this.ActionType].Extensions.Count;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (service.Ports[service.Name+this.ActionType].Extensions[j] is SoapAddressBinding)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{                                                                                        
InBlock.gif                                sd[i].ServiceAddress
=((SoapAddressBinding)service.Ports[service.Name+this.ActionType].Extensions[j]).Location;
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch(SystemException se)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MessageBox.Show(se.Message);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
//服务为,服务地址,服务的协议,服务提供的方法,服务方法要传递的参数
InBlock.gif                    
//获取,port所对应binding
InBlock.gif
                    System.Web.Services.Description.Binding bindGet=wsdl.Bindings[service.Ports[service.Name+this.ActionType].Binding.Name];
InBlock.gif                    
//OperationBinding function=null;
InBlock.gif
                    for(int k=0;k<bindGet.Operations.Count;k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        fd
=new FunctionDescription(bindGet.Operations[k].Name);
InBlock.gif                        fd.FunctionAddress
=sd[i].ServiceAddress+"/"+bindGet.Operations[k].Name;
InBlock.gif                        sd[i].Functions.Add(fd);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    PortType ptGet
=wsdl.PortTypes[bindGet.Type.Name];
InBlock.gif                    Operation op
=null;
InBlock.gif                    System.Web.Services.Description.Message msg
=null;
InBlock.gif                    
for(int temp=0;temp<ptGet.Operations.Count;temp++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ArrayList msgList
=new ArrayList();
InBlock.gif                        
for(int pp=0;pp<ptGet.Operations[temp].Messages.Count;pp++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
InBlock.gif                            
if(ptGet.Operations[temp].Messages[pp] is OperationInput)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                msgList.Add(wsdl.Messages[ptGet.Operations[temp].Messages[pp].Message.Name]);
InBlock.gif
ExpandedSubBlockEnd.gif                            }

InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
string res="";
InBlock.gif                        res
+="方法:"+ptGet.Operations[temp].Name+"的参数说明如下:\n";
InBlock.gif                        res
+="输入的参数个数为:"+msgList.Count+"个;\n他们分别为:\n";
InBlock.gif
InBlock.gif                        
for(int iii=0;iii<msgList.Count;iii++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            msg
=(System.Web.Services.Description.Message)msgList[iii];
InBlock.gif                            
for(int jj=0;jj<msg.Parts.Count;jj++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
if(msg.Parts[jj].Element.IsEmpty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    res
+="参数的类型为:"+stringSpilt(msg.Parts[jj].Type.ToString())+"  参数名称为:"+msg.Parts[jj].Name;
InBlock.gif                                    res
+="\t\r\n";
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    res
+="参数的类型为:"+stringSpilt(msg.Parts[jj].Element.ToString())+"  参数名称为:"+msg.Parts[jj].Name;
InBlock.gif                                    res
+="\t\r\n";
InBlock.gif
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        MessageBox.Show(res,
"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }
        以上就是核心的代码,主要是利用.NET自动生成的WSDL文档格式的特点,对文档进行解析,同时得到Web服务中所有函数的元数据信息,以便进一步对其进行调用。
        在调用时,我这里使用的是HTTP POST协议。
        具体的大家有看不太懂得可以给我留言
        本人是出学,请大家指教

转载于:https://www.cnblogs.com/qeilf/archive/2006/05/30/412936.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值