http调用wsdl

 

 String wsUrl = "http://172.16.15.109:1010/projectName/wsauth/userWSService";
 
 public String ddd()throws Exception{
  //服务的地址
        URL url = new URL(wsUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
       
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
       
        //请求体---使用soapui获取
        StringBuffer sb = new StringBuffer();
  sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wss=\"http://wsservice.userInfo.uias.jit.com/\">");
  sb.append("<soapenv:Header/>");
  sb.append("<soap:Body>");
  sb.append("<wss:deleteOrg>");
  sb.append("<orgId>ID1</orgId>");
  sb.append("</wss:deleteOrg>");
  sb.append("</soapenv:Body>");
  sb.append("</soapenv:Envelope>");
  String soap = sb.toString();
       
        OutputStream os = conn.getOutputStream();
  os.write(soap.getBytes("UTF-8"));

  InputStream is = conn.getInputStream();
       
        byte[] b = new byte[1024];
  int len = 0;
  String s = "";
  while ((len = is.read(b)) != -1) {
   String ss = new String(b, 0, len, "UTF-8");
   s += ss;
  }
        System.out.println(s);
       
        is.close();
        os.close();
        conn.disconnect();
       
        return s;
 }
 

 

 

Java调用WSDL有多种实现方法,下面为你介绍常见的两种方式。 ### 手动编码调用 可以通过使用Apache Axis库手动编写代码来调用WSDL形式发布的Web Service。以下是一个示例代码: ```java import java.util.Date; import java.text.DateFormat; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName; import java.lang.Integer; import javax.xml.rpc.ParameterMode; public class caClient { public static void main(String[] args) { try { // 直接引用远程的wsdl文件 String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); // WSDL里面描述的接口名称 call.setOperationName("addUser"); // 接口的参数 call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE, javax.xml.rpc.ParameterMode.IN); // 设置返回类型 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); String temp = "测试人员"; // 给方法传递参数,并且调用方法 String result = (String)call.invoke(new Object[]{temp}); System.out.println("result is " + result); } catch (Exception e) { System.err.println(e.toString()); } } } ``` 在上述代码中,首先创建了一个`Service`对象,接着通过`Service`对象创建`Call`对象,然后设置目标端点地址、操作名称、参数和返回类型,最后调用`invoke`方法来执行Web Service操作。这种方式适用于对调用过程有精细控制需求的场景,不过需要手动处理很多细节,如参数类型、命名空间等。 ### 利用wsdl地址生成Java客户端调用类 有时需要和外部的Webservice系统对接,可根据对方的wsdl地址生成Java客户端调用类并且动态配置访问地址。此方法可以减少手动编写代码的工作量,尤其是在WSDL文件比较复杂时,能提高开发效率。不过需要掌握生成工具的使用,并且可能需要对生成的代码进行一定的调整和配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值