. NET call Java side with ws-security supported Web Service "pro-Test Pass"

Source: Internet
Author: User
Tags soap wsdl

Did a few years of development, today finally summon up the courage to open the blog park. Usually are looking for all kinds of Daniel, see their share of the blog to solve the bug. From today onwards, I also began to share my way of learning. Also hope that everyone support!

Recently received a AXIS2 implementation of the WebService through the. NET docking needs, began to be quite simple thing, after the general reference call, the test does not pass, need to verify the password. After... Start a crazy search for code.

Scenario One: https://www.cnblogs.com/yf2011/p/5465558.html

This program makes me try hard, WSE 2.0, WSE 3.0 is the old version of VS (I use vs 2017), I tried a bit WSE 3.0, did not succeed. You can try it, VS2017 NuGet package with WSE 3.0, it should be possible.

My plan:

Reference address 1:46909395

Reference Address 2:https://stackoverflow.com/questions/734355/clueless-about-how-to-create-soap-wssesecurity-header

Reference Address 3:40340819

The first thing I'm using is a WCF configuration scenario. Reference address 1 resolves how the WSDL address generates a client-callable. cs file through the WCF tools.

Operation Steps:

  1. Open the Developer Command prompt tool for VS 2017
  2. Enter SvcUtil.exe http://localhost:6054/ServiceDemo.svc?wsdl, and a direct carriage return will generate two files. cs,. config.
  3. . cs files are copied directly into the project, and. config is changed to your project's app. config replacement. Open App. Config file
    <?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <System.ServiceModel>        <Bindings>            <BasicHttpBinding>                <bindingname= "Data1binding" />            </BasicHttpBinding>            <custombinding>                <bindingname= "Data2binding">                    <textmessageencodingMessageVersion= "SOAP12" />                    <HttpTransport/>                </binding>            </custombinding>        </Bindings>        <Client>            <EndpointAddress= "http://XXX1.com/"binding= "BasicHttpBinding"bindingconfiguration= "Data1binding"Contract= "Dataserviceporttype"name= "Dataservicehttpsoap11endpoint" />            <EndpointAddress= "http://XXX2.com/"binding= "CustomBinding"bindingconfiguration= "Data2binding"Contract= "Dataserviceporttype"name= "Dataservicehttpsoap12endpoint" />        </Client>    </System.ServiceModel></Configuration>

    Mining Pit One: it's probably like I have two addresses. I'm sorry. You can only use one, delete one.
    Mining Pit Two: If your WCF is a single item, add the contents of the. config file to the . config file of the main project.

  4. Add a Securityheader class with the following code:
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacecode{ Public classSecurityHeader:System.ServiceModel.Channels.MessageHeader { Public stringUserName;  Public stringpassword;  PublicSecurityheader (stringNamestringPSW) {UserName=name; Password=PSW; }        protected Override voidOnwritestartheader (System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion MessageVersion) {writer. WriteStartElement ("Wsse", Name, Namespace); Writer. Writexmlnsattribute ("Wsse", Namespace); }        protected Override voidOnwriteheadercontents (System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion MessageVersion) {writer. WriteStartElement ("Wsse","UsernameToken", Namespace); Writer. WriteStartElement ("Wsse","Username", Namespace); Writer.            WriteValue (UserName); Writer.            WriteEndElement (); Writer. WriteStartElement ("Wsse","Password", Namespace); Writer. WriteAttributeString ("Type","Http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"); Writer.            WriteValue (password); Writer.            WriteEndElement (); Writer.        WriteEndElement (); }         Public Override stringName {Get{return "Security"; } }         Public Override stringNamespace {Get{return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; } }    }}

    The primary role of this class is to manually create a SOAP protocol request header, referring to address 2 as original. refer to Address 3 is the Java version of the code I found, should be able to explain why this is written.

  5. Call the method with the following code:
     Public Static voidMain (string[] args) {        varWebService =NewServicereference1.mywebservice ();//your webservice.Webservice.open (); using(OperationContextScope scope =NewOperationContextScope ((IContextChannel) webservice.innerchannel)) {messageheaders messageheaderselement=OperationContext.Current.OutgoingMessageHeaders; Messageheaderselement.add (NewSecurityheader ("UserName","Password"))             varres = Webservice.myserve ("Method Parameters");//the method in WebServiceConsole.WriteLine (Res.        ToString ()); }    }

This allows you to generate tests that are not as simple as it feels. But I was doing it for three days. First write, write bad place, everyone please understand!

. NET call Java side with ws-security supported Web Service "pro-Test Pass"

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.