一个很简单的WSDL调用,不使用框架,就是个很轻量级的Client,主要功能是检查QQ号是否在线。
import java.io.*;
import java.net.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class HttpClientWS {
public static void main(String[] args) throws Exception {
String soapRequestData = ""
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">"
+ " <soap:Body>"
+ " <qqCheckOnline xmlns=\"http://WebXml.com.cn/\">"
+ " <qqCode>396738007</qqCode>"
+ " </qqCheckOnline>"
+ " </soap:Body>"
+ "</soap:Envelope>";
URL u = new URL("http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl");
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(soapRequestData);
pw.close();
DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = bf.newDocumentBuilder();
Document document = db.parse(uc.getInputStream());
String res = document.getElementsByTagName("qqCheckOnlineResponse").item(0).getTextContent();
System.out.println(res);
}
}