Calling. net's wcf using java is actually quite simple. java calls. netwcf

Source: Internet
Author: User

Calling. net's wcf using java is actually quite simple. java calls. netwcf

A few days ago, I was connected to one of our email service providers. The two parties needed some communication. The other party was a java team. As a. net team, we had to publish a wcf basicbinding.

When the problem comes, the other party does not know how to call this basic. Maybe they are a little bit of food, a little embarrassed. I have to study it. In fact, as long as I know the published wsdl, it is very easy for any language.

 

I. Case studies

For convenience, I did not specifically write any code. I used the wcf Service template in vs to create it. The details are as follows:

1 // Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "Service1" in the code, svc, and configuration file at the same time ". 2 // Note: To start the WCF test client to test this service, select Service1.svc or Service1.svc. cs in Solution Explorer and start debugging. 3 public class Service1: IService1 4 {5 public string GetData (int value) 6 {7 return string. format ("You entered: {0}", value); 8} 9 10 public CompositeType GetDataUsingDataContract (CompositeType composite) 11 {12 if (composite = null) 13 {14 throw new ArgumentNullException ("composite"); 15} 16 if (composite. boolValue) 17 {18 composite. stringValue + = "Suffix"; 19} 20 return composite; 21} 22}

 

Run F5 and you will see that such a wcf test client has a service address and two method contracts provided.

 

 

Ii. java call

1. Use web service client

In fact, there is a Web Service Client template in the new eclipse project, which can convert the wsdl to the java code of the proxy, one thing is like the Service reference in C #, isn't it,

Next we will perform the following operations step by step:

1. You can create a java project or a dynamic web project. In this test, only one MyJavaTest project is created.

 

2. Use the WebService Client template File => Other => Web Services => Web Service Client.

 

3. In Service definition, enter our wcf Service address: http: // localhost: 54248/Service1.svc? Pay attention to wsdl.

Add a wsdl to the end of svc, so that the java proxy can find it. Then there is a "thermometer" in the lower left corner, which is adjusted to the start client mode.

OK, and then click Next. For details, see:

 

4 indicates the directory in which the generated code will be put. There is nothing to say.

 

5 and click finish. Now you will find that you have several more automated java classes, making it easier to compete with visual studio.

 

All right, the automated classes are coming out. Next, I just need to create a new entry's Program class. Can I fix it...

 1 package com.datamip; 2  3 import java.rmi.RemoteException; 4  5 import org.tempuri.IService1Proxy; 6  7 public class Program { 8  9     public static void main(String[] args) throws RemoteException {10 11         IService1Proxy proxy = new IService1Proxy();12 13         String result = proxy.getData(12345);14 15         System.out.println(result);16     }17 18 }

But what I want to say is that IService1Proxy is actually a class. I thought it was an interface. How can I think about it ~~~

 

2. Use the wsimport Tool

Java JDK has many small tools, including wsimport.exe, which can be used to generate automated proxy classes based on wsdl and. net svcutil functions.

Is the same.

 

Since jdk paths are configured in environment variables, you can check the parameters required by wsimport in cmd.

C: \ Users \ hxc> wsimport lacks WSDL_URI. Usage: wsimport [options] <WSDL_URI> \ [options] includes: -B <path> specifies the jaxws/jaxb binding file or additional mode (each <path> must have its own-B) -B <jaxbOption> transmits this option to the JAXB mode compiler-catalog <file> to specify that the directory files used to parse external entity references support TR9401, XCatalog, and OASIS XML directory formats. -D <directory> specifies the location where the generated output file is placed-encoding <encoding> specifies the character encoding used by the source file-extension allows vendor extension-not specified according to specifications. Using extensions may cause applications to be unportable or unable to interoperate with other implementations.-help display help-httpproxy: 

In this case, you can probably know the following four parameters.

1 url: You need to generate the proxy data source, that is, the wsdl File

2-d: Path to store the generated proxy File

3-p: generate the package name of the proxy class.

4-keep: Save the generated file name

Next, let's give it a try in cmd:

C: \ Users \ hxc> wsimport http: // localhost: 54248/Service1.svc? Wsdl-d c: \ 2 \-p com. datamip. proxy-keep is parsing WSDL... generating code... compiling code... c: \ Users \ hxc>

 

Now, copy the proxy class in "folder 2" to the project, for example:

 

Does it look perfect? The next thing to do is coding verification ~~~

 

Finally, there is a stupid way to capture webservice communication parameters through fiddler and simulate submission through httppost... Okay. I will talk to you in this article.

Here, I hope to help you.

 

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.