一个很简单的Java调用WSDL示例

SOAP, WSDL, JAVA

一个很简单的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 = ""
+ ""
+ ""
+ "  "
+ "   "
+ "    396738007"
+ "  

+ " 
"
+ "
";

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);
}
}

你可能感兴趣的:(webservice)