springboot调用c#的soap接口

java如何调用c#开发的soap接口呢

这里就给大家讲一下,如何调用

先引入jar

<dependency>
          <groupId>wsdl4j</groupId>
          <artifactId>wsdl4j</artifactId>
          <version>1.6.3</version>
      </dependency>
<dependency>
   <groupId>org.apache.axis</groupId>
   <artifactId>axis</artifactId>
   <version>1.4</version>
</dependency>

<dependency>
   <groupId>org.apache.axis</groupId>
   <artifactId>axis-jaxrpc</artifactId>
   <version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.73</version>
</dependency>

<dependency>
   <groupId>javax.activation</groupId>
   <artifactId>activation</artifactId>
   <version>1.1.1</version>
</dependency>

<dependency>
   <groupId>commons-discovery</groupId>
   <artifactId>commons-discovery</artifactId>
   <version>0.2</version>
</dependency>
复制代码

编写代码,调用接口

   static String SOAP_URL = "http://xxxxx:6044/TBRXG.asmx";
   static String SOAP_ACTION = "http://xxxxxxxx/GetXG_CS_";
    /**
     * 
     * @param request
     * @param model
     * @return
     */
   @RequestMapping("/soap2")
   public  String soap2(HttpServletRequest request,Model model) {
  
      String code=request.getParameter("code");
      if(code.equals(""))
      {
         model.addAttribute("value", "条码不能为空");
         return "typecode";
      }
      try {
         java.net.URL url = new java.net.URL(SOAP_URL);
         java.net.URLConnection connection = url.openConnection();
         java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connection;
         byte[] b = getInputXML("104", "EgLEAgY89N0=", code).getBytes();
         httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
         httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
         httpConn.setRequestProperty("SOAPAction", SOAP_ACTION);
         httpConn.setRequestMethod("POST");
         httpConn.setDoOutput(true);
         httpConn.setDoInput(true);

         java.io.OutputStream outStm = httpConn.getOutputStream();
         outStm.write(b);
         outStm.close();

         java.io.InputStreamReader isrStm = new java.io.InputStreamReader(httpConn.getInputStream());
         java.io.BufferedReader inStm = new java.io.BufferedReader(isrStm);
         String inputLine;
         inputLine = inStm.readLine();
         inStm.close();
         if (inputLine == null) {
//             return null;
         }
         String res = inputLine.substring(inputLine.indexOf("<GetXG_CS_Result>") + "<GetXG_CS_Result>".length(),
               inputLine.indexOf("</GetXG_CS_Result>"));
//       System.out.println("---" + res);
         String type = res.substring(res.indexOf("<TyreSpecCode>") + "<TyreSpecCode>".length(),
               res.indexOf("</TyreSpecCode>"));
         String typeName=res.substring(res.indexOf("<TyreSpecName>") + "<TyreSpecName>".length(),
               res.indexOf("</TyreSpecName>"));
//       System.out.println("TyreSpecCode:   " + type);
//       System.out.println("TyreSpecName:   " + typeName);
//       model.addAttribute("value", type);
         model.addAttribute("value", type+"-----"+typeName);
         return "typecode";
      } catch (Exception e) {
         // TODO: handle exception
      }

      return null;
   }
   
   
   
//解析xml数据

   private static String getInputXML(String factoryId, String deviceId, String tmh) {

      StringBuffer sb = new StringBuffer();
      sb.append("<?xml version="1.0" encoding="utf-8"?>");
      sb.append(
            "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">");
      sb.append("<soap:Body><GetXG_CS_ xmlns="http://tempuri.org/">");
      sb.append("<factoryId>").append(factoryId).append("</factoryId>");
      sb.append("<deviceId>").append(deviceId).append("</deviceId>");
      sb.append("<tmh>").append(tmh).append("</tmh>");
      sb.append("</GetXG_CS_></soap:Body></soap:Envelope>");
      return sb.toString();
   }
复制代码

以上就是java调用soap接口的代码了,希望可以帮到大家