- package axis2;
- import org.apache.axiom.om.OMAbstractFactory;
- import org.apache.axiom.om.OMElement;
- import org.apache.axiom.om.OMFactory;
- import org.apache.axiom.om.OMNamespace;
- import org.apache.axis2.Constants;
- import org.apache.axis2.addressing.EndpointReference;
- import org.apache.axis2.client.Options;
- import org.apache.axis2.client.ServiceClient;
- /**
- * Check QQ Online
- *
- */
- public class QqOnlinewebServiceDemo {
- private static EndpointReference targetEPR =
- new EndpointReference("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx");
- private static OMFactory factory = OMAbstractFactory.getOMFactory();
- private static OMNamespace omNs = factory.createOMNamespace("http://WebXml.com.cn/", "tns");
- private OMElement qqCheckOnlinePayLoad(String qqCode){
- OMElement method = factory.createOMElement("qqCheckOnline",omNs);
- OMElement value = factory.createOMElement("qqCode",omNs);
- value.addChild(factory.createOMText(value, qqCode));
- method.addChild(value);
- return method;
- }
- @SuppressWarnings("finally")
- public String checkQQOnline(String qqCode) {
- String result = "查询失败!";
- OMElement qqCheckOnlinePayLoad = null;
- Options options = null;
- ServiceClient sender = null;//发送器
- OMElement receive = null;//接收返回数据
- try {
- qqCheckOnlinePayLoad = qqCheckOnlinePayLoad(qqCode);
- options = new Options();
- options.setTo(targetEPR);//Set target
- options.setAction("http://WebXml.com.cn/qqCheckOnline");//set SOAPAction
- options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
- sender = new ServiceClient();
- sender.setOptions(options);
- receive = sender.sendReceive(qqCheckOnlinePayLoad);//发送并接受数据
- String response = receive.getFirstElement().getText();
- if("Y".equalsIgnoreCase(response)){
- result = "在线";
- }else if("N".equalsIgnoreCase(response)){
- result = "离线";
- }else if("E".equalsIgnoreCase(response)){
- result = "QQ号码错误";
- }
- } catch (Exception e) {
- String error_message = ".........QQOnlineWebService:checkQQOnline......未知异常,查询失败..........";
- System.out.println(error_message);
- System.out.println("........QQOnlineWebService:checkQQOnline..."+e.getMessage());
- throw new RuntimeException(error_message);
- }finally{
- return result;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/260320149122.html
来源: http://www.codesnippet.cn/detail/260320149122.html