Home  >  Article  >  Backend Development  >  Using Java to call WebService

Using Java to call WebService

巴扎黑
巴扎黑Original
2016-12-19 16:50:361283browse

This is an example of calling the C# version of the WebService interface using Java:
C# interface:

<span style="font-size: 11px;">
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
[WebService(Namespace = "http://www.tangs.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () ...{
//如果使用设计的组件,请取消注释以下行 
//InitializeComponent(); 
}
 [SoapRpcMethod(Action = "http://www.tangs.com/Add", RequestNamespace = "http://www.tangs.com/T", ResponseNamespace = "http://www.tangs.com/T", Use = SoapBindingUse.Literal)]
 [WebMethod]
public int Add(int a, int b)
...{
return a + b;
 }
 [SoapRpcMethod(Action = "http://www.tangs.com/Hello", RequestNamespace = "http://www.tangs.com/T", ResponseNamespace = "http://www.tangs.com/T", Use = SoapBindingUse.Literal)]
 [WebMethod]
public String HelloWorld()
...{
return "Hello, world!";
 }
}
...</span>

Java calling the add method and HelloWorld method in this web service:

1, parameter method: add

<span style="font-size: 11px;">
public static void addTest() {
try ...{
 Integer i = 1;
 Integer j = 2;
//WebService URL
 String service_url = "http://localhost:4079/ws/Service.asmx";
 Service service = new Service();
 Call call = (Call) service.createCall();
 call.setTargetEndpointAddress(new java.net.URL(service_url));
//设置要调用的方法
call.setOperationName(new QName("http://www.tangs.com/T", "Add"));
//该方法需要的参数
call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT,
 javax.xml.rpc.ParameterMode.IN);
 call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT,
 javax.xml.rpc.ParameterMode.IN);
//方法的返回值类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
 call.setUseSOAPAction(true);
 call.setSOAPActionURI("http://www.tangs.com/Add");
//调用该方法
Integer res = (Integer)call.invoke(
new Object[]...{
 i, j
 }
 );
 System.out.println( "Result: " + res.toString());
 } catch (Exception e) ...{
 System.err.println(e);
 }
 }...
</span>

runs, and the result returns: Result: 3

2. No-parameter method: HelloWorld

<span style="font-size: 11px;">
public static void helloTest() {
try ...{
 String endpoint = "http://localhost:4079/ws/Service.asmx";
 Service service = new Service();
 Call call = (Call) service.createCall();
 call.setTargetEndpointAddress(new java.net.URL(endpoint));
 call.setOperationName(new QName("http://www.tangs.com/T", "HelloWorld"));
 call.setUseSOAPAction(true);
 call.setSOAPActionURI("http://www.tangs.com/Hello");
 String res = (String)call.invoke(
new Object[]...{
null
 }
 );
 System.out.println( "Result: " + res);
 } catch (Exception e) ...{
 System.err.println(e.toString());
 }
 }...
</span>

You can see that calling the web service without parameters is basically the same as the one with parameters, but when calling without parameters, there is no need to call The addParameter method and setReturnType method
execute the query query results report to view: Hello, world!

The attachment is in the JAR package that the web service depends on


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:.net open fileNext article:.net open file