在java中使用SOAP web服务

在java中使用SOAP web服务,java,web-services,soap,Java,Web Services,Soap,我正在寻找一些在java中使用SOAP web服务的替代方案。我目前正在使用存根方法来使用它,它对于我的讲师来说太简单了。我的指导老师说,要做一个简单的客户端,这意味着什么?SOAP基本上是使用POST方法向web服务器提交XML。虽然XML可能会变得冗长,但您应该能够使用StringBuilder构造XML,然后使用一个简单的HTTP客户机,如使用 XML字符串作为主体 这就简单多了。下面是一个使用soap api的简单而轻量级的示例。步骤如下 SoapRequestBuilder s = n

我正在寻找一些在java中使用SOAP web服务的替代方案。我目前正在使用存根方法来使用它,它对于我的讲师来说太简单了。我的指导老师说,要做一个简单的客户端,这意味着什么?

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


这就简单多了。

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

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();
必须创建SOAPTestController.java、KflConstants.java和SoapClient.java类

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

下面是SOAPTestController.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";
}
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();
        }
    }
下面是SOAPClient.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";
}
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();
        }
    }

谢谢,

我不太明白一个微不足道的客户意味着什么。。。因此,让客户端尽可能地哑巴?我想说的是,指导者试图让您不要使用大型框架,而是坚持简单的任务,即获取服务器调用服务所需的最少信息。HttpClient站点有一个示例目录,其中一个是PostSOAP。有没有办法查看我的存根客户端发送给服务的soap消息?在端点上使用像tcpmon这样的拦截软件来获取消息。