• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
肥肥大超
博客园    首页    新随笔    联系   管理    订阅  订阅

一个很简单的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 = ""
+ "<?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);
}
}
勇气,信念,坚持
posted @ 2014-02-07 18:19  sheroHuo  阅读(1427)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3