【发布时间】:2011-10-11 16:43:01
【问题描述】:
我需要创建一个应用程序来从该服务http://www.mcds.co.il/YouTube/ChanelApi.asmx 获取 xml 响应,而无需其他库,但我不知道该怎么做。请帮帮我
【问题讨论】:
-
有很多使用 Java 创建 SOAP 客户端的教程和库。
我需要创建一个应用程序来从该服务http://www.mcds.co.il/YouTube/ChanelApi.asmx 获取 xml 响应,而无需其他库,但我不知道该怎么做。请帮帮我
【问题讨论】:
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
【讨论】:
您可以使用 apache 的 Axis 生成 SOAP 客户端代码,请参阅“使用 Web 服务”部分。明确查看正在发生的事情的最佳方法是使用 Axis 附带的 WSDL2Java 工具来生成客户端存根。这将为您构建一个 SOAP 客户端,您可以查看模型对象并开始针对它们进行开发。
WSDL2Java 将 WSDL URL 作为输入,并为该 WSDL 生成一个 java 客户端。
【讨论】: