The following editor will bring you an example of a Java client calling .NET WebService. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
The project needs to call .NET’s WebSrevice, which is Java. After studying it for a long time, I finally got some clues and wrote them down.
1, create a new .NET WebService. Just add a string type parameter str
[WebMethod] public string HelloWorld(string str) { return "Hello World"; }
2 to the original method, create a new Java WebService client, and introduce the following 5 jar packages into lib (I used the WebService client generated by idea, which will download 7 packages. I tried to delete the log4j and saaj packages and it still runs normally)
import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; public class HelloWorldClient { public static void main(String[] argv) { String endpoint ="http://localhost:64662/WebService1.asmx?wsdl"; try { // 定义服务 Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName(new QName("http://tempuri.org/", "HelloWorld")); call.setSOAPActionURI("http://tempuri.org/HelloWorld"); call.addParameter(new QName("http://tempuri.org/", "str"),// 这里的str对应webservice参数名称 XMLType.SOAP_STRING, ParameterMode.IN); call.setReturnType(XMLType.SOAP_STRING); String retVal1 = (String) call.invoke(new Object[] {"Hello World!"}); System.out.println(retVal1); } catch (Exception e) { e.printStackTrace(); } } }
Note:
1, I saw it written directly as call.setOperationName("HelloWorld") online. Tried to no avail. I don’t know if it’s because of cross-language issues.
2, I also saw the sentence omitting call.setSOAPActionURI online, but I reported an error.
3. In fact, the parameters used in the project's WebService are entities. I tried to pass the entity through the XMLType.XSD_ANYTYPE type on the Java side, but the result was that the type was not registered. There are solutions online that are quite cumbersome. It is better to serialize the entities into Json and pass them through to save trouble.
4. For the parameter namespace, please refer to the service page
The above is the detailed content of How does Java call an instance of WebService in .NET?. For more information, please follow other related articles on the PHP Chinese website!