开发者

How to get response from WS SOAP service in Java?

I need to create an app for getting xml response from this service http://www.mcds.co.il/YouTube/ChanelApi.a开发者_运维百科smx without additional libraries, but I don't know how can I do it. Please help me


    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 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜