C # Access to Java WebService add SoapHeader validation issues

Source: Internet
Author: User
Tags soap wsdl

These two days to do with the company OA interface, found that C # access to Java WebService need to provide a SOAP header authentication information, but the WebService WSDL does not provide the contract validation information, so the visual Studio generated the proxy class did not find these content, so this soapheader must be self-organizing, but also encountered a problem is the SOAP header information, OA side also according to do not know what instance added a root node does not have a namespace, This led to the SoapHeader content has been reported transmission errors, nonsense not much to say, on the two stages of the example. First, create a Requestsoapheader class that inherits from System.Web.Services.Protocols.SoapHeader, where you add username and password two properties, with the following code: Public class Requestsoapheader: System.Web.Services.Protocols.SoapHeader{     Public stringUser {Get; Set; }     Public stringPassword {Get; Set; }}the generated WebService proxy class is set as an attribute member: Public Partial class Isysnotifytodowebserviceservice{     Public RequestsoapheaderRequestsoapheader {Get; Set; }}Add the SoapHeader declaration above the accessed WebService method:[System.Web.Services.Protocols.Soapdocumentmethodattribute("", requestnamespace="http://webservice.notify.sys/", responsenamespace="http://webservice.notify.sys/" , Use=system.web.services.description.SoapBindingUse. Literal, Parameterstyle=system.web.services.protocols.Soapparameterstyle. Wrapped)][return: System.Xml.Serialization.XmlElementAttribute("Return", Form=system.xml.schema.XmlSchemaForm . Unqualified)] [ soapheader( "Requestsoapheader")] Public NotifytodoappresultSettododone ([System.Xml.Serialization.XmlElementAttribute (Form=system.xml.schema.XmlSchemaForm. Unqualified)]Notifytodoremovecontextarg0) {     Object[] results = This. Invoke ("Settododone", New Object[] {arg0});     return ((Notifytodoappresult) (results[0]));}The sending message is as follows: The result returns the user name password error: Check OA system to access the document, only to find that soapheader need to be placed in special namespace content, but because the original WSDL may not have any explicit declaration for security reasons, After some toss finally understand how to transform the WebService proxy class adds a custom namespace and prefix to the message node. The original message requirements are as follows: <requestsoapheader xmlns:tns= "Http://sys.webservice.client" >
<tns:UserName>
Username
</tns:UserName>
<tns:Password>
Password
</tns:Password>
</RequestSOAPHeader> change the original message header object Requestsoapheader to: Public class Requestsoapheader: System.Web.Services.Protocols.SoapHeader{         PublicRequestsoapheader ()        {namespaces =New XmlSerializerNamespaces();        }        [XmlElement(Namespace ="Http://sys.webservice.client")]         Public stringusername{Get; Set; }        [XmlElement(Namespace ="Http://sys.webservice.client")]         Public stringPassword {Get; Set; }        [xmlnamespacedeclarations]         Public XmlSerializerNamespacesNamespaces {Get; Set; }}then, when instantiating the Requestsoapheader object in the constructor of the proxy class, add the required namespace definition to the namespaces:Requestsoapheader =NewOA.Requestsoapheader();RequestSOAPHeader.Namespaces.Add ("TNS", "Http://sys.webservice.client" );Requestsoapheader.username=settings.default.connectusername;Requestsoapheader.password=settings.default.connectpassword;once the transformation is complete and then accessed, the following message is generated:The display was sent successfully.

C # Access to Java WebService add SoapHeader validation issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.