根据客户端程序语言及调用方式不同,设置的方法也不同,下面示例说明客户端程序语言为java调用方式为动态调用的设置方法:用org.apache.axis.client.Call 的addHeader方法:
call.addHeader(new SOAPHeaderElement("Authorization","username",username));
call.addHeader(new SOAPHeaderElement("Authorization","password",password));
其他的调用方式及其他语言设置方式请查阅Axis相关文档..请问大侠,,c#.net中怎么调用呢??没有有类似的方法?我已经建立了一个 webservice引用了。。可是不知道怎么设置soapheader,他们把身份验证都放在 soapheader里了。
call.addHeader(new SOAPHeaderElement("Authorization","username",username));
call.addHeader(new SOAPHeaderElement("Authorization","password",password));
其他的调用方式及其他语言设置方式请查阅Axis相关文档..请问大侠,,c#.net中怎么调用呢??没有有类似的方法?我已经建立了一个 webservice引用了。。可是不知道怎么设置soapheader,他们把身份验证都放在 soapheader里了。
实例webservice的时候进行身份验证
他应该把这个写在构造函数里了
比如命名空间和类为A和B
则可以A.B operate=new A.b();
operate.方法()
那个operate可以自己换url
service.MySoapHeaderValue = new WSDemoClient.localhost.MySoapHeader();
service.MySoapHeaderValue.Username = "a";
service.MySoapHeaderValue.Password = "b";
service.CookieContainer = new System.Net.CookieContainer(); service.Login("a", "a");
align]
public MySoapHeader header;
// 自定义的 SOAPHeader
public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{
public string Username;
public string Password;
}在方法名上添加[System.Web.Services.Protocols.SoapHeader("header")]
soapHeader头部
7楼的你试试吧。
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{ // 自定义的 MySoapHeader 类型的数据成员
public MySoapHeader header;
public Service () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod(EnableSession=true)]
[System.Web.Services.Protocols.SoapHeader("header")]
public void Login(string username, string password)
{
if( this.CheckUser( username, password))
{
// set id
this.Session["Login"] = "Ok";
}
} [System.Web.Services.Protocols.SoapHeader("header")]
[WebMethod(EnableSession=true)]
public string HelloWorld() {
object o = this.Session["Login"];
if (o != null)
{
return "Hello World";
}
else
{
throw new Exception("no access");
}
} [WebMethod(EnableSession=true)]
public DateTime GetServerTime()
{
object o = this.Session["Login"];
if (o!=null)
{
System.Threading.Thread.Sleep(4000);
return DateTime.Now;
}
else
{
throw new Exception("no access");
}
}
private bool CheckUser(string username, string password)
{
if (username == password)
return true;
else
return false;
}}// 自定义的 SOAPHeader
public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{
public string Username;
public string Password;
}
客户端using System;
using System.Collections.Generic;
using System.Text;namespace WSDemoClient
{
class Program
{
static void Main(string[] args)
{
localhost.Service service = new WSDemoClient.localhost.Service(); string url = service.Url; Console.WriteLine(url); // 在客户端实例 MySoapHeader 对象
service.MySoapHeaderValue = new WSDemoClient.localhost.MySoapHeader();
service.MySoapHeaderValue.Username = "a";
service.MySoapHeaderValue.Password = "b";
service.CookieContainer = new System.Net.CookieContainer(); service.Login("a", "a");
string hello = service.HelloWorld(); Console.WriteLine(hello); DateTime now = service.GetServerTime(); Console.WriteLine(now); Console.ReadLine();
}
}
}
<s:element name="MySoapHeader" type="tns:MySoapHeader" />
<s:complexType name="MySoapHeader"> //这个是有MySoapHeader类
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
</s:sequence>
<s:anyAttribute />
</s:complexType>//Login方法上添加了 [System.Web.Services.Protocols.SoapHeader("header")]
<wsdl:message name="LoginMySoapHeader">
<wsdl:part name="MySoapHeader" element="tns:MySoapHeader" />
</wsdl:message>
对比看下吧
<soap12:operation soapAction="http://tempuri.org/Login" style="document" />
<wsdl:input>
<soap12:body use="literal" />
<soap12:header message="tns:LoginMySoapHeader" part="MySoapHeader" use="literal" /> </wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:intf="http://server.webservice.core.epm" xmlns:impl="http://server.webservice.core.epm" targetNamespace="http://server.webservice.core.epm" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types />
<wsdl:message name="originalInvokeRequest">
<wsdl:part name="path" type="xsd:string" />
<wsdl:part name="methodName" type="xsd:string" />
<wsdl:part name="dataXmlStr" type="xsd:string" />
</wsdl:message>
<wsdl:message name="invokeResponse">
<wsdl:part name="invokeReturn" type="xsd:string" />
</wsdl:message>
<wsdl:message name="invokeRequest">
<wsdl:part name="path" type="xsd:string" />
<wsdl:part name="methodName" type="xsd:string" />
<wsdl:part name="dataXmlStr" type="xsd:string" />
</wsdl:message>
<wsdl:message name="originalInvokeResponse">
<wsdl:part name="originalInvokeReturn" type="xsd:anyType" />
</wsdl:message>
<wsdl:message name="invokeServiceResponse">
<wsdl:part name="invokeServiceReturn" type="xsd:string" />
</wsdl:message>
<wsdl:message name="invokeServiceRequest">
<wsdl:part name="path" type="xsd:string" />
<wsdl:part name="methodName" type="xsd:string" />
<wsdl:part name="dataXmlStr" type="xsd:string" />
</wsdl:message>
<wsdl:portType name="GenericServer">
<wsdl:operation name="invoke" parameterOrder="path methodName dataXmlStr">
<wsdl:input name="invokeRequest" message="impl:invokeRequest" />
<wsdl:output name="invokeResponse" message="impl:invokeResponse" />
</wsdl:operation>
<wsdl:operation name="invokeService" parameterOrder="path methodName dataXmlStr">
<wsdl:input name="invokeServiceRequest" message="impl:invokeServiceRequest" />
<wsdl:output name="invokeServiceResponse" message="impl:invokeServiceResponse" />
</wsdl:operation>
<wsdl:operation name="originalInvoke" parameterOrder="path methodName dataXmlStr">
<wsdl:input name="originalInvokeRequest" message="impl:originalInvokeRequest" />
<wsdl:output name="originalInvokeResponse" message="impl:originalInvokeResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="GenericServerSoapBinding" type="impl:GenericServer">
<wsdlsoap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
<wsdl:operation name="invoke">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="invokeRequest">
<wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:input>
<wsdl:output name="invokeResponse">
<wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="invokeService">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="invokeServiceRequest">
<wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:input>
<wsdl:output name="invokeServiceResponse">
<wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="originalInvoke">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="originalInvokeRequest">
<wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:input>
<wsdl:output name="originalInvokeResponse">
<wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="GenericServerService">
<wsdl:port name="GenericServer" binding="impl:GenericServerSoapBinding">
<wsdlsoap:address location="http://172.19.4.170:7010/web/services/GenericServer" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>以上是 GenericServer.wsdl的代码。。他要求的最终 传输的SOAP头信息如下:
最终传输的SOAP头信息如下:
<soapenv:Header>
<ns1:username
soapenv:actor="http://schemas.xmlsoap.org/soap/actor/nex"
soapenv:mustUnderstand="0" xsi:type="soapenc:string"
xmlns:ns1="Authorization" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
username
</ns1:username>
<ns2:password
soapenv:actor="http://schemas.xmlsoap.org/soap/actor/nex"
soapenv:mustUnderstand="0" xsi:type="soapenc:string" xmlns:ns2="Authorization" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
password
</ns2:password>
</soapenv:Header>
楼主,搞定了么? 还有 zhouguofang1981 这个你搞定了么?