qeilf

博客园 首页 新随笔 联系 订阅 管理
HttpWebRequest httpRequest;
            
try
            
{
            
                httpRequest
=(HttpWebRequest)WebRequest.Create(this.cbox_url.Text);

            }

            
catch(SystemException se)
            
{
                MessageBox.Show(
this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
return;
            }

            httpRequest.Method
="GET";
            HttpWebResponse httpResponse;
            
try
            
{
            
                httpResponse
=(HttpWebResponse)httpRequest.GetResponse();
            }

            
catch(SystemException se)
            
{    
                MessageBox.Show(
this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
return;
            }

            ServiceDescription wsdl;
            
try
            
{
                wsdl
=ServiceDescription.Read(httpResponse.GetResponseStream());
            }

            
catch(SystemException se)
            
{
                MessageBox.Show(
this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
return;
            }

            
finally
            
{
                httpResponse.Close();

            }

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

                        }

                    }

                    
catch(SystemException se)
                    
{
                        MessageBox.Show(se.Message);
                    }

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

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

                            }


                        }

                        
string res="";
                        res
+="方法:"+ptGet.Operations[temp].Name+"的参数说明如下:\n";
                        res
+="输入的参数个数为:"+msgList.Count+"个;\n他们分别为:\n";

                        
for(int iii=0;iii<msgList.Count;iii++)
                        
{
                            msg
=(System.Web.Services.Description.Message)msgList[iii];
                            
for(int jj=0;jj<msg.Parts.Count;jj++)
                            
{
                                
if(msg.Parts[jj].Element.IsEmpty)
                                
{
                                    res
+="参数的类型为:"+stringSpilt(msg.Parts[jj].Type.ToString())+"  参数名称为:"+msg.Parts[jj].Name;
                                    res
+="\t\r\n";
                                }

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

                                }

                            }

                            
                        }

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

                    
                }

            }
        以上就是核心的代码,主要是利用.NET自动生成的WSDL文档格式的特点,对文档进行解析,同时得到Web服务中所有函数的元数据信息,以便进一步对其进行调用。
        在调用时,我这里使用的是HTTP POST协议。
        具体的大家有看不太懂得可以给我留言
        本人是出学,请大家指教
posted on 2006-05-30 13:45  chen lei  阅读(3455)  评论(1编辑  收藏  举报