您的位置:首页 > 其它

Web服务入门之三:通过XFire调用远程Web Service,实现QQ在线服务功能

2010-06-09 19:18 981 查看
通过一段时间对Web Service的使用,还是发现了Web Service并不像之前想象的那样。毕竟程序也是人写的,思想可以完美,但实现起来却无法达到那样的境界。

两个月以前就说过要写XFire调用远程WEB Service的程序,这中间也试着写过,总是出问题。我的总结是,XFire在调用远程.NET的Web Service的时候还是存在问题,或者说在使用的时候并不是那么的顺手。如下面代码中写的,test1方法在调用的时候是存在问题的,而test2却可以正常调的通。

import java.net.URL;
import java.util.Properties;

import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;

public class Test
{
public static void main(String[] args)
{
test2();
}

public static void test1()
{
setProxy();
Service srvcModel = new CustomServiceFactory().create(QQOnlineTest.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
try
{
String qqCheckURL = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx";
QQOnlineTest srvc = (QQOnlineTest) factory.create(srvcModel, qqCheckURL);
String result = srvc.qqCheckOnline("315***");
System.out.println(result);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void test2()
{
try
{
setProxy();
Client client = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
Object[] results = client.invoke("qqCheckOnline", new String[] { "315***" });
System.out.println(results[0]);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void setProxy()
{
Properties prop = System.getProperties();
prop.setProperty("http.proxyHost", "myproxy");
prop.setProperty("http.proxyPort", "8080");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐