今晨浏览论坛, 看到帖子http://topic.csdn.net/u/20081217/13/5a9075fc-feb1-4cde-96ac-5f982f7a1b7b.html. 此贴公布了一些免费且常用的WebService. 由于本人从来没有使用过WebService, 顿感好奇, 遂开始研究. 于是根据上贴中的一个天气预报服务, 写了如下三个方法去调用该webServicepublic static String invokeWebServiceBySoap() throws IOException{
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
connection.connect();
String param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
+"<soap12:Body>"
+"<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
+"<theCityName>北京</theCityName>"
+"</getWeatherbyCityName>"
+"</soap12:Body>"
+"</soap12:Envelope>";
param = new String(param.getBytes(), "UTF-8");
OutputStream os = connection.getOutputStream();
os.write(param.getBytes("UTF-8"));
os.flush();
os.close();
InputStream in = connection.getInputStream();
byte[] bs = new byte[in.available()];
in.read(bs);
in.close();
os.close();
return new String(bs, "UTF-8");
}
public static String invokeWebServiceByHttpGet() throws IOException{
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=北京");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream in = connection.getInputStream();
byte[] bs = new byte[in.available()];
in.read(bs);
in.close();
return new String(bs, "UTF-8");
}
public static String invokeWebServiceByHttpPost() throws IOException{
URL url = new URL("http://www.webxml.com.cn//WebServices/WeatherWebService.asmx/getWeatherbyCityName");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();
String city = "theCityName=北京";
city = new String(city.getBytes(), "UTF-8");
OutputStream os = connection.getOutputStream();
os.write(city.getBytes("UTF-8"));
os.flush();
os.close();
InputStream in = connection.getInputStream();
byte[] bs = new byte[in.available()];
in.read(bs);
in.close();
os.close();
return new String(bs, "UTF-8");
}三个方法分别使用HTTP+SOAP/POST, HTTP/GET, HTTP/POST 三种方式去调用此service, 执行都能正确返回结果, 可是由于没有接触过WebServie, 所以有如下两个疑问:(1). 既然后上述三个方法都能调用WebService, 为何还要是用Axis或者XFire编写WebService客户端?
(2). 直接在服务器上发布一个Servlet, 然后响应给客户端一个xml结构的输出流. 这样的servlet与WebService有和区别?我想可能是我根本就没理解什么是WebService, 所以请熟悉WebService的朋友帮忙指点一下, 不胜感激!
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
connection.connect();
String param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
+"<soap12:Body>"
+"<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
+"<theCityName>北京</theCityName>"
+"</getWeatherbyCityName>"
+"</soap12:Body>"
+"</soap12:Envelope>";
param = new String(param.getBytes(), "UTF-8");
OutputStream os = connection.getOutputStream();
os.write(param.getBytes("UTF-8"));
os.flush();
os.close();
InputStream in = connection.getInputStream();
byte[] bs = new byte[in.available()];
in.read(bs);
in.close();
os.close();
return new String(bs, "UTF-8");
}
public static String invokeWebServiceByHttpGet() throws IOException{
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=北京");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream in = connection.getInputStream();
byte[] bs = new byte[in.available()];
in.read(bs);
in.close();
return new String(bs, "UTF-8");
}
public static String invokeWebServiceByHttpPost() throws IOException{
URL url = new URL("http://www.webxml.com.cn//WebServices/WeatherWebService.asmx/getWeatherbyCityName");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();
String city = "theCityName=北京";
city = new String(city.getBytes(), "UTF-8");
OutputStream os = connection.getOutputStream();
os.write(city.getBytes("UTF-8"));
os.flush();
os.close();
InputStream in = connection.getInputStream();
byte[] bs = new byte[in.available()];
in.read(bs);
in.close();
os.close();
return new String(bs, "UTF-8");
}三个方法分别使用HTTP+SOAP/POST, HTTP/GET, HTTP/POST 三种方式去调用此service, 执行都能正确返回结果, 可是由于没有接触过WebServie, 所以有如下两个疑问:(1). 既然后上述三个方法都能调用WebService, 为何还要是用Axis或者XFire编写WebService客户端?
(2). 直接在服务器上发布一个Servlet, 然后响应给客户端一个xml结构的输出流. 这样的servlet与WebService有和区别?我想可能是我根本就没理解什么是WebService, 所以请熟悉WebService的朋友帮忙指点一下, 不胜感激!
解决方案 »
- 有谁知道JSF1.2和JSF2.0在标签组件上有哪些区别,公司开晨会要每个人都讲。希望高手指点,因为本人刚进公司对1.2和2.0都不怎么熟悉,让人纠结。
- 登陆判断有效期的问题
- 请问关于clone和equals
- 如何将jsp和java代码分离?
- jdbc中结果集resultset问题
- 关于spring的问题 !! 急 ,解决就给分
- hibernate查询数据库没有问,但写数据库就出现下面错误
- 哪种 java IDE 好些啊?
- 推荐java开发栏目,有数百篇开发资料
- How can I debug jsp in IDE such as JBuilder using Resin as App Server ?
- 请教Java多线程,生产者消费者问题如何解决消费者不消费的问题
- soap与web service
webservice 实际应用 要复杂的多。
所以才出现axis 和xfire 这种类库来封装
楼主所列的三种方法为传统方法,这样做的结果是开发难度大,程序很难维护。大致列几个web service的优点:
一:跨防火墙的通信
二:应用程序集成
三:B2B的集成
四:软件和数据重用
2.web service 通信一般都会有2个消息 发送消息和接受消息的回馈
如果是servlet 只能单一的返回消息 无法进行一些业务判断
web service 可以跨语言通信
照你这样的说法很多类库都不需要使用了,javamail啊,poi啊,dom4j啦,都可以自己实现这些功能嘛
我只是使用过axis1和axis2,就axis来说发布一个webservice以及编写一个客户端是很方便的,比如说Lz按照你的方法你所调用的方法需要传递一个list,map或者自己定义的对象的话你改怎么实现呢?用axis就可以很方便的实现
(2)webservice不是servlet
http://www.docin.com/p-27546561.html
1.主要是为了提高开发效率
2.并不是说所有的webservice是给客户端调用的,有一大部分仍然是java后台系统调用的。
3.楼主上述三个方法都能调用,但有没有发现三个调用都有重复的代码,为了代码的优化便产生了webservic框架
4.返回的xml文件解析也是头痛的问题,用框架封装后易于调用。
同意
推荐使用xfrie