>  기사  >  백엔드 개발  >  Java를 사용하여 WebService 호출

Java를 사용하여 WebService 호출

巴扎黑
巴扎黑원래의
2016-12-19 16:50:361285검색

다음은 Java를 사용하여 C# 버전의 WebService 인터페이스를 호출하는 예입니다.
C# 인터페이스:

<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>

이 웹 서비스에서 add 메소드와 HelloWorld 메소드를 호출하는 Java:

1. 매개변수가 있는 메소드:

<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>

을 추가하고 실행하면 결과가 반환됩니다. 결과: 3

2. 매개변수가 없는 메소드: 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>

보시다시피 매개변수 없이 웹 서비스를 호출하는 것은 기본적으로 매개변수를 사용하여 웹 서비스를 호출하는 것과 같습니다. 그러나 매개변수 없이 호출하는 경우에는 addParameter 메서드와 setReturnType 메서드를 호출할 필요가 없습니다.
쿼리 쿼리 실행 결과 보고서 보기: Hello, world !

첨부파일은 웹서비스가 의존하는 JAR 패키지입니다


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:.net 열린 파일다음 기사:.net 열린 파일