Java calls the WebService interface of. Net

Source: Internet
Author: User

To invoke WebService, you first have to have an interface, with the interface address already written in MyEclipse Target project, right-->new the Web service client->

Select Jax-ws mode, click "Next", see the following interface: Enter the WebService interface address, and then select the package that you want to generate the client, do not select default is the project's defaults package.

Click "Next", enter the verification link, click "Next" again, enter the following interface: If new project is a Web service project, these two items do not need to be checked, otherwise you have to tick.

Finally click "Finish" to complete. This generates the WebService client.

Next, the WebService interface of. NET is called in the target Project program, and the recommended way is to configure Adapterdomain (equals wsdllocation value) in the resource file, NamespaceURI ( equals the value of targetnamespace), the Localpart (equals the name of the service) three variables, which is better for the portability of the program, for example: Define these three variables in the Default.properties file:

adapterdomain=http://192.168.1.104/ssd/adapterservice/adapterservice.asmx?wsdlnamespaceuri=http://tempuri.org/ Localpart=adapterservice

It should be declared that these three variables can be found in the @webserviceclient class of the HTTP://192.168.1.104/SSD/ADAPTERSERVICE/ADAPTERSERVICE.ASMX?WSDL interface address or the generated client's annotations.

Then, create a new Callwebservice class in this project:

1  Public classCallwebservice {2     StaticLogger Logger = Logger.getlogger (callwebservice.class. GetName ());3     4     PrivateString Adapterdomain;5     PrivateString NamespaceURI;6     PrivateString Localpart;7     8      PublicCallwebservice (String adapterdomain,string namespaceuri,string localpart) {9         Super();Ten          This. Adapterdomain =Adapterdomain; One          This. NamespaceURI =NamespaceURI; A          This. Localpart =Localpart; -     } -  PublicAdapterserviceportbinding Initportbind ()throwsexception{ theAdapterserviceportbinding Adapterservice =NULL; - URL url; -URL =NewURL (adapterdomain); -QName QName =NewQName (namespaceuri,localpart); +Javax.xml.ws.Service Service =javax.xml.ws.Service.create (URL, qName); -Adapterservice = Service.getport (adapterserviceportbinding.class); +         returnAdapterservice; A     } at      -  -      PublicString GetAll ()throwsException { -         returnInitportbind (). GetAll (); -}

Adapterserviceportbinding is a class that exposes interface methods, and different formulations may have different results. Here the method GetAll () is the method declared in the interface, and is published to the network in the Adapterserviceportbinding class.

At this point, we can invoke the method of this interface in the program.

But before that, write a Util tool class that reads the resource file: Propertiesutil

1  Public classPropertiesutil {2 3     StaticLogger Logger = Logger.getlogger (propertiesutil.class. GetName ());4     Private StaticPropertiesutil instance =NULL;5     Private StaticString file =NULL;6     PrivateProperties props =NewProperties ();7     Private StaticInputStream in =NULL;8 9      Public Static synchronizedpropertiesutil getinstance () {Ten         if(Instance = =NULL) { OneInstance =Newpropertiesutil (); A         } -         returninstance; -     } the  -      PublicPropertiesutil () { -     } -  +      Public voidInit (String inputfile) { -         if(!inputfile.equals (file)) { +File =Inputfile; A             Try { at //In = new Bufferedinputstream (new FileInputStream (Inputfile)); -in = Propertiesutil.class. getClassLoader (). getResourceAsStream (inputfile); - props.load (in); -}Catch(Exception e) { -Logger.error ("Error: Configuration file operation failed, initialization of property file failed"); - Logger.error (e); inSystem.out.println ("Error: Configuration file operation failed, initializing property file failed \ n" + dateutil.getinstance (). Formatterdate (NewDate ()) + "" +getclass (). GetName ()); -                 //e.printstacktrace (); to             } +         } -     } the  *      Publicstring ReadValue (String key) { $String value =Props.getproperty (key);Panax Notoginseng         returnvalue; -}

OK, now it's ready to call.

For example, I write this in the UserService class:

1  PublicString findauthortreestr () {2        //call the method to get the Callwebservice object and call the interface method3List List = This. GetService (). GetAll ();4     5}6 7     /**8 * Call WebService, get instance9      * @returnTen      */ One  PublicCallwebservice GetService () { APropertiesutil Propertiesutil =propertiesutil.getinstance (); -Propertiesutil.init ("Default.properties"); -Adapterdomain =Propertiesutil.readvalue (adapterdomain); theNamespaceURI =Propertiesutil.readvalue (NamespaceURI); -Localpart =Propertiesutil.readvalue (localpart); -          -Callwebservice Service =NewCallwebservice (Adapterdomain, NamespaceURI, Localpart); +         returnService; -}

This completes a webservice call. The note ends.

Java calls the WebService interface of. Net

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.