javawebservice:如何用Java访问WEB Service来源: 发布时间:星期三, 2008年12月17日 浏览:68次 评论:0
最近在学习Web Service发现了个国内Web Service提供站点其中最简单是查询QQ在线状态服务我通过Java直接发送SOAP请求文件访问Web Service成功这种方式实现比较简单不需要第 3方软件Software包
import java.io.*; import java.net.*; QQOnlineService { public void (String args) throws Exception { String urlString = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx"; String xmlFile = "QQOnlineService.XML"; String soapActionString = "http://WebXml.com.cn/qqCheckOnline"; URL url = URL(urlString); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection;; File fileToSend= File(xmlFile); buf= [()fileToSend.length]; FileInputStream(xmlFile).read(buf); httpConn.RequestProperty( "Content-Length",String.valueOf( buf.length ) ); httpConn.RequestProperty("Content-Type","text/xml; char=utf-8"); httpConn.RequestProperty("soapActionString",soapActionString); httpConn.RequestMethod( "POST" ); httpConn.DoOutput(true); httpConn.DoInput(true); OutputStream out = httpConn.getOutputStream; out.write( buf ); out.close; InputStreamReader isr = InputStreamReader(httpConn.getInputStream,"utf-8"); BufferedReader in = BufferedReader(isr); String inputLine; BufferedWriter bw= BufferedWriter( OutputStreamWriter( FileOutputStream("result.xml"))); while ((inputLine = in.readLine) != null){ .out.prln(inputLine); bw.write(inputLine); bw.Line; } bw.close; in.close; } } 用到 QQOnlineService.XML文件可以通过预先访问http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx得到 查询结果文件如下对其进步编程可以实现更为灵活查询功能 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <qqCheckOnlineResponse xmlns="http://WebXml.com.cn/"> <qqCheckOnlineResult>N </qqCheckOnlineResult> </qqCheckOnlineResponse> </soap:Body> </soap:Envelope> TAG: Service web service 0
相关文章
读者评论发表评论 |
|