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

时间:2011-10-11 16:43:02

标签: java soap

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

    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