HttpWebRequest httpRequest;
try
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
httpRequest=(HttpWebRequest)WebRequest.Create(this.cbox_url.Text);
![](/Images/OutliningIndicators/InBlock.gif)
}
catch(SystemException se)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
MessageBox.Show(this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
httpRequest.Method="GET";
HttpWebResponse httpResponse;
try
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
httpResponse=(HttpWebResponse)httpRequest.GetResponse();
}
catch(SystemException se)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
MessageBox.Show(this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
ServiceDescription wsdl;
try
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
wsdl=ServiceDescription.Read(httpResponse.GetResponseStream());
}
catch(SystemException se)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
MessageBox.Show(this,se.Message,"Read WSDL Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
finally
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
httpResponse.Close();
![](/Images/OutliningIndicators/InBlock.gif)
}
//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")
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.ActionType=SOAP;
this.AddressBindingString="SoapAddressBinding";
for(int i=0;i<servicesCount;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
service=wsdl.Services[i];
sd[i]=new ServicesDescription();
sd[i].ServiceName=service.Name;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int j=0;j<service.Ports[service.Name+this.ActionType].Extensions.Count;j++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (service.Ports[service.Name+this.ActionType].Extensions[j] is SoapAddressBinding)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sd[i].ServiceAddress=((SoapAddressBinding)service.Ports[service.Name+this.ActionType].Extensions[j]).Location;
break;
}
}
}
catch(SystemException se)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ArrayList msgList=new ArrayList();
for(int pp=0;pp<ptGet.Operations[temp].Messages.Count;pp++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(ptGet.Operations[temp].Messages[pp] is OperationInput)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
msgList.Add(wsdl.Messages[ptGet.Operations[temp].Messages[pp].Message.Name]);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
string res="";
res+="方法:"+ptGet.Operations[temp].Name+"的参数说明如下:\n";
res+="输入的参数个数为:"+msgList.Count+"个;\n他们分别为:\n";
![](/Images/OutliningIndicators/InBlock.gif)
for(int iii=0;iii<msgList.Count;iii++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
msg=(System.Web.Services.Description.Message)msgList[iii];
for(int jj=0;jj<msg.Parts.Count;jj++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(msg.Parts[jj].Element.IsEmpty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
res+="参数的类型为:"+stringSpilt(msg.Parts[jj].Type.ToString())+" 参数名称为:"+msg.Parts[jj].Name;
res+="\t\r\n";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
res+="参数的类型为:"+stringSpilt(msg.Parts[jj].Element.ToString())+" 参数名称为:"+msg.Parts[jj].Name;
res+="\t\r\n";
![](/Images/OutliningIndicators/InBlock.gif)
}
}
}
MessageBox.Show(res,"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
以上就是核心的代码,主要是利用.NET自动生成的WSDL文档格式的特点,对文档进行解析,同时得到Web服务中所有函数的元数据信息,以便进一步对其进行调用。
在调用时,我这里使用的是HTTP POST协议。
具体的大家有看不太懂得可以给我留言
本人是出学,请大家指教