如何在Java中从WS-SOAP服务获取响应?
我需要创建一个应用程序来从这个服务获取xml响应,而不需要额外的库,但我不知道如何才能做到这一点。请帮助我您可以使用apache生成SOAP客户端代码,请参阅“使用web服务”部分。明确了解发生了什么的最好方法是使用Axis附带的WSDL2Java工具来生成客户端存根。这将为您构建一个SOAP客户机,您可以查看模型对象并开始针对它们进行开发 WSDL2Java将WSDL URL作为输入,并为该WSDL生成java客户端。如何在Java中从WS-SOAP服务获取响应?,java,soap,Java,Soap,我需要创建一个应用程序来从这个服务获取xml响应,而不需要额外的库,但我不知道如何才能做到这一点。请帮助我您可以使用apache生成SOAP客户端代码,请参阅“使用web服务”部分。明确了解发生了什么的最好方法是使用Axis附带的WSDL2Java工具来生成客户端存根。这将为您构建一个SOAP客户机,您可以查看模型对象并开始针对它们进行开发 WSDL2Java将WSDL URL作为输入,并为该WSDL生成java客户端。URL URL=newURL(“http://www.mcds.co.il/
URL URL=newURL(“http://www.mcds.co.il/YouTube/ChanelApi.asmx");
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
//生成xml
字符串数据=“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“字符串\r\n”+
“\r\n”+
“\r\n”+
"";
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”、“文本/xml”);
conn.setRequestProperty(“内容长度”,Integer.toString(data.getBytes().Length));
conn.setRequestProperty(“SOAPAction”,“\”http://tempuri.org/GetChanel\"");
conn.SETUSECHACHES(假);
连接设置输出(真);
conn.setDoInput(真);
DataOutputStream wr=新的DataOutputStream(
conn.getOutputStream());
wr.writeBytes(数据);
wr.flush();
wr.close();
最终字符[]缓冲区=新字符[0x10000];
StringBuilder out=新的StringBuilder();
Reader in=新的InputStreamReader(连接getInputStream(),“UTF-8”);
int-read;
做{
read=in.read(缓冲区,0,缓冲区长度);
如果(读取>0){
追加(缓冲区,0,读取);
}
}而(读取>=0);
System.out.println(out);
//解析出
有很多教程和库用于在Java中创建SOAP客户端。