如何从 Java 中的 WS SOAP 服务获取响应?

发布于 2024-12-09 04:18:35 字数 174 浏览 0 评论 0原文

我需要创建一个应用程序来从该服务 http://www.mcds 获取 xml 响应。 co.il/YouTube/ChanelApi.asmx 没有额外的库,但我不知道该怎么做。请帮我

I need to create an app for getting xml response from this service http://www.mcds.co.il/YouTube/ChanelApi.asmx without additional libraries, but I don't know how can I do it. Please help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生殊途 2024-12-16 04:18:35
    URL url = new URL("http://www.mcds.co.il/YouTube/ChanelApi.asmx");


    //generate your xml 
    String data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + 
            "<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/\">\r\n" + 
            "  <soap:Body>\r\n" + 
            "    <GetChanel xmlns=\"http://tempuri.org/\">\r\n" + 
            "      <CategoryName>string</CategoryName>\r\n" + 
            "    </GetChanel>\r\n" + 
            "  </soap:Body>\r\n" + 
            "</soap:Envelope>";


    HttpURLConnection  conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "text/xml");

    conn.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
    conn.setRequestProperty("SOAPAction","\"http://tempuri.org/GetChanel\"");

    conn.setUseCaches (false);
    conn.setDoOutput(true);
    conn.setDoInput(true);

    DataOutputStream wr = new DataOutputStream (
            conn.getOutputStream ());
    wr.writeBytes(data);
    wr.flush ();
    wr.close ();

    final char[] buffer = new char[0x10000];
    StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(conn.getInputStream(), "UTF-8");
    int read;
    do {
      read = in.read(buffer, 0, buffer.length);
      if (read>0) {
        out.append(buffer, 0, read);
      }
    } while (read>=0);
    System.out.println(out);

    //parse out 
    URL url = new URL("http://www.mcds.co.il/YouTube/ChanelApi.asmx");


    //generate your xml 
    String data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + 
            "<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/\">\r\n" + 
            "  <soap:Body>\r\n" + 
            "    <GetChanel xmlns=\"http://tempuri.org/\">\r\n" + 
            "      <CategoryName>string</CategoryName>\r\n" + 
            "    </GetChanel>\r\n" + 
            "  </soap:Body>\r\n" + 
            "</soap:Envelope>";


    HttpURLConnection  conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "text/xml");

    conn.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
    conn.setRequestProperty("SOAPAction","\"http://tempuri.org/GetChanel\"");

    conn.setUseCaches (false);
    conn.setDoOutput(true);
    conn.setDoInput(true);

    DataOutputStream wr = new DataOutputStream (
            conn.getOutputStream ());
    wr.writeBytes(data);
    wr.flush ();
    wr.close ();

    final char[] buffer = new char[0x10000];
    StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(conn.getInputStream(), "UTF-8");
    int read;
    do {
      read = in.read(buffer, 0, buffer.length);
      if (read>0) {
        out.append(buffer, 0, read);
      }
    } while (read>=0);
    System.out.println(out);

    //parse out 
遮了一弯 2024-12-16 04:18:35

您可以使用 apache 的 Axis 生成 SOAP 客户端代码,请参阅“使用网络服务”部分。明确查看正在发生的情况的最佳方法是使用 Axis 附带的 WSDL2Java 工具来生成客户端存根。这将为您构建一个 SOAP 客户端,您可以查看模型对象并开始针对它们进行开发。

WSDL2Java 将 WSDL URL 作为输入,并为该 WSDL 生成 java 客户端。

You can use apache's Axis to generate SOAP client code, see the "Consuming a web service" section. The best way to see explicitly what is happening is to use the WSDL2Java tool that ships with Axis, to generate client stubs. This will build a SOAP client for you, and you can take a look at the model objects and start to develop against them.

WSDL2Java takes a WSDL URL as an input, and generates a java client for that WSDL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文