有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中使用SOAP web服务

我正在寻找一些在java中使用SOAP web服务的替代方案。我目前正在使用存根方法来使用它,它对于我的讲师来说太简单了。我的导师说做一个琐碎的客户,那是什么意思


共 (3) 个答案

  1. # 1 楼答案

    下面是使用soap api的简单而轻量级的示例。步骤如下

    1. 您必须创建SOAPTestController。java,KflConstants。java和SoapClient。java类

    2. 然后实现下面的代码块并享受它

    这是SOAPTestController。java类

    @Controller
    public class SOAPTestController {
    
        @RequestMapping(value = "/showdate", method = RequestMethod.GET)
        public @ResponseBody String getDateAndTime() {
    
            String DateAndTimeSOAPRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
                    + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\r\n"
                    + "  <soap12:Body>\r\n" + "    <GetDateAndTime xmlns=\"http://tempuri.org/\" />\r\n"
                    + "  </soap12:Body>\r\n" + "</soap12:Envelope>";
            String Fundtion = "GetDateAndTime";
            return new SoapClient().ConsumeTheService(DateAndTimeSOAPRequest, "GetDateAndTime");
        }
    }
    

    这是KflConstants。java类

    public class KflConstants {
    
        public static final String SERVER_IP = "http://192.168.0.222/";
        public static final String SERVICE_URL = SERVER_IP + "businesswebserviceNew/service.asmx";
        public static final String CONTENT_TYPE_TEXT_XML = "text/xml; charset=utf-8";
        public static final String GET_DATE_AND_TIME_URL = SERVICE_URL + "/GetDateAndTime";
    }
    

    这是SOAPClient。java类

    public class SoapClient {
    
        private static Logger log = LogManager.getLogger(SoapClient.class);
    
        /*Input Stream Convert to the String Object*/
        public static String convertStreamToString(java.io.InputStream is) {
            java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
            return s.hasNext() ? s.next() : "";
        }
    
        public String ConsumeTheService(String SOAPXML, String APINAME) {
            String Result = null;
            try {
                /*Create The Connection*/
                URL url = new URL(KflConstants.SERVICE_URL);
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestProperty("Content-Type", KflConstants.CONTENT_TYPE_TEXT_XML);
                conn.setRequestProperty(APINAME, KflConstants.GET_DATE_AND_TIME_URL);
                log.info("Sending the envelope to server");
                /*Send the request XML*/
                OutputStream outputStream = conn.getOutputStream();
                outputStream.write(SOAPXML.getBytes());
                outputStream.close();
                /* Read the response XML*/
                log.info("Reading the Response");
                InputStream inputStream = conn.getInputStream();
                Result = convertStreamToString(inputStream);
                inputStream.close();
                /*INput Stream Convert to the SOAP Message*/
                InputStream is = new ByteArrayInputStream(Result.getBytes());
                SOAPMessage resposeSOAP = MessageFactory.newInstance().createMessage(null, is);
                /*Return Values*/
                log.info("Result SOAP:"+resposeSOAP.toString());
                log.info("Result String:"+Result);
                return Result;
    
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e);
                return e.toString();
            }
        }
    

    谢谢

  2. # 2 楼答案

    SoapRequestBuilder s = new SoapRequestBuilder();
    s.Server = "127.0.0.1"; // server ip address or name
    
    s.MethodName = "ConcatWithSpace";
    s.XmlNamespace = "http://tempuri.org/";
    s.WebServicePath = "/SimpleService/Service1.asmx";
    s.SoapAction = s.XmlNamespace+s.MethodName;
    s.AddParameter("one", "David");
    s.AddParameter("two", "Hobbs");
    String response = s.sendRequest();
    
  3. # 3 楼答案

    SOAP基本上是使用POST方法向web服务器提交XML。虽然XML可能会变得冗长,但您应该能够使用StringBuilder构造XML,然后使用一个简单的HTTP客户端,如Apache HttpClient,使用 XML字符串作为主体

    这就跟他们说的一样简单