Webservice实验报告

更新时间:2023-10-22 20:59:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

J I A N G S U U N I V E R S I T Y

题目:Web Services 实验报告

所属学院: 计算机学院 专业班级: 软件1201 姓 名: 孙磊 学 号: 3120608016

1

题目一:

调用第三方web Service的接口实现便民信息的查询,例如天气预报或者火车信息。

一. 实验要求

编写客户端,调用第三方Web Service提供的接口,实现对基本信息的查询。

二. 运行环境

Windows8 + Eclipse( JDK 8.0)

三.实验原理及过程

1.新建Java Project,项目名称为“天气查询”,新建包 com.soap(放置跟web service服务调用相关的类)和包com.ui(放置跟UI界面编程相关的类)。

2.在com.soap包下新建类名为WeatherServices的Java文件。该类的作用调用中国天气网提供的免费接口\,实现对城市的天气的实时查询。

项目结构如图:

中间用到的方法主要有: (1)getSoapRequest(String city).这个方法的作用是得到一个 Soap请求。 (2)getSoapInputStream(String city )。 这个方法是通过调用上述免费接口获得 URLConnection连接对象,并对Soap 输入流进行相关的设置,包括编码格式等。

(3)getWeather(String city)。解析xml,获取相应的信息。由于查询后返回的原始结果是一个xml文档,因此需要对该文档进行DOM解析,从而获得我们需要的信息。

3.在com.soap包下新建MobileService.java文件,用来实现对手机号码基本信息的查询。使用的接口是:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asm x?wsdl

2

4.在com.soap包下新建EmailService.java,实现对邮箱地址的验证。采用的接口是: http://www.webxml.com.cn/WebServices/ValidateEmailWebService.asmx?wsdl\

5.在com.ui包下新建类名为MainFrame的Java文件。实现客户端的可视化布局。 里面设计的主要函数包括:

(1)MainFrame(String string)。这是一个构造方法,对窗体进行简单的初始化,以及为相应的控件添加事件监听及响应。

四.运行效果

3

4

题目二

自定义web service,并将其发布,然后编写客户端,调用该web service。

一. 实验要求

编写客户端,调用第三方Web Service提供的接口,实现对基本信息的查询。

二. 运行环境

Windows 7旗舰版 + Eclipse( JDK 8.0)

三.实验原理及过程

该实验需要自己编写相应的服务器端和客户端。而我要实现的功能是在客户端窗体输入学号,然后通过调用web service来返回学号对应的学生的姓名。实现服务的发布的方式有很多种,例如用Axis2来实现。其实,JDK本身就提供了Web Service服务发布的方法,就是jax-ws。具体实现的过程如下:

1.服务器端

(1)新建Java Project,项目名称为:webServervices_student_server。在该项下新建包com.webservice.server。

(2) 在包中新建一个接口StudentInterface,在接口中声明一个查询的方法studentQuery(int number);

(3)新建StudentInterface接口的实现类StudentInterfaceImp,在类中实现StudentInterface接口中的查询方法studentQuery(int number)。Main方法中的Endpoint.publish(\/student\new StudentInterfaceImp())方法是发布服务。其中http://127.0.0.1:54321/student是在本地自定义的RUL,54321是服务的端口号。

(4)运行该程序,会看到会打印以下提示信息,说明该服务已经启动:

(5)接下来要做的就是要生成WSDL文件。在浏览器地址栏中输入http://127.0.0.1:54321/student?wsdl,回车,会看到以下所示界面,说明已经将服务发布成功。

5

2.客户端

(1)为了模拟远程调用,新建Java Project,项目名称为:webServervices_student_client。在该项下新建包com.webservice.client。由于采用的是jax-ws,所以在客户端还需要用到按照上述的wsdl规格约束编译的 .java文件。具体的实现如下:

1)新建一个java 项目wsimport,该项目可以用来专门放置其他项目的编译文件。找到该项目的src的绝对目录F:\\Eclipse for javaee\\wsipport\\src.

2)在cmd命令窗口运行cd F:\\Eclipse for javaee\\wsipport\\src,进入该目录下; 3)接着输入命令:wsimport –s . http://127.0.0.1:54321/student?wsdl。注意中间的空格,回车后,结果如:

6

(2)返回wsimport项目,右键刷新,会看到多出来一个包,这个包的名字和之前的服务器端的包名一致。将该包原封不动复制到该客户端的src目录下。 (3)新建Client窗体类,实现客户端的编程。整个客户端的项目结构如下: 7

四.运行效果

8

实验感想

第一次接触Web Service这个概念,感觉还是挺陌生的。之前有听说过便民查询服务,而且也亲身用过。查阅了很多资料,才知道这些功能就是用Web Service来实现的。大概了解了相关的知识和调用第三方的服务接口的流程后,才知道,一个服务的发布和调用,也是很简单的。当然在整个过程中,还遇到过多次的失败, 虽然不是很难的程序,但是如果其中某一步没有按照正确的顺序来执行,会很容易出错的。以下就来说说遇到的有一些问题: 1.在发布服务的时候,未能成功的生成wsdl文件。分析原因主要有URL的端口没有设置好,或者服务端的程序编写有误。当服务开启后,也就是服务端程序成功执行后,再在浏览器的地址栏里输入url+?wsdl,会成功生成。

2.在解析命令窗口解析wsdl的时候,不会生成.java文件,或者生成的.java文件所在的包名和原先的服务程序的包名不一致,说明解析有误,需要重新解析。在输入命令的时候,wsimport –s . http://127.0.0.1:54321?wsdl中间的空格不能少。

虽然这次的实验实现的功能比较简单,但是它却涵盖了一个完整的webservice的发布和调用的全过程,有了这次的实践。那么以后再做比较复杂的服务的时候也就不用担心没有头绪了。

附录:源代码(天气查询 + 自定义服务)

1.天气查询:

(1)WeatherService.java

package com.soap;

import java.io.InputStream; import java.io.OutputStream;

import java.io.OutputStreamWriter; import java.net.URL;

import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList;

public class WeatherServices { private static String getSoapRequest(String city) {

9

StringBuilder builder = new StringBuilder(); builder.append(\ +\2001/XMLSchema-instance\\\ + \2001/XMLSchema\\\ + \ + \ \ + \ return builder.toString(); }

private static InputStream getSoapInputStream(String city) throws Exception { try { String soap = getSoapRequest(city); if (soap == null) { return null; } URL url = new URL( \ URLConnection connection = url.openConnection(); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestProperty(\ connection.setRequestProperty(\ connection.setRequestProperty(\ \ OutputStream out = connection.getOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out, \ writer.write(soap); writer.flush(); writer.close(); out.close(); InputStream in = connection.getInputStream(); return in; } catch (Exception e) { e.printStackTrace(); return null; } }

public static String getWeather(String city) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder();

10

}

}

InputStream is = getSoapInputStream(city); Document doc = builder.parse(is);

//因为web services调用后返回的xml的每个元素都是string NodeList list = doc.getElementsByTagName(\ StringBuffer buffer = new StringBuffer(); for(int i=0;i

is.close();

return buffer.toString(); } catch (Exception e) { e.printStackTrace(); return null; }

(2)MobileService.java

package com.soap;

import cn.com.webxml.MobileCodeWS; import cn.com.webxml.MobileCodeWSSoap; public class MobileService{ public static String getMobileCodeInfo(String tele,String userID) { MobileCodeWS codeWS = new MobileCodeWS(); MobileCodeWSSoap codeWSSoap = codeWS.getMobileCodeWSSoap(); String codeInfo = codeWSSoap.getMobileCodeInfo(tele, null); System.out.println(codeInfo); return codeInfo; } }

(3)EmailService.java

package com.soap;

import cn.com.webxml.ValidateEmailWebService; import cn.com.webxml.ValidateEmailWebServiceSoap;

public class EmailService{ public static String chekEmailAddress(String email){

11

ValidateEmailWebService emailWebService = new ValidateEmailWebService(); ValidateEmailWebServiceSoap serviceSoap emailWebService.getValidateEmailWebServiceSoap(); short result = serviceSoap.validateEmailAddress(email); String info=\ switch (result) { case 0: info = \请重新验证\ case 1: }

}

info = \邮件地址合法\case 2: info = \只是域名正确\case 3:

info = \一个未知错误\case 4: info = \邮件服务器没有找到\case 5:

info = \电子邮件地址错误\case 6: info = \免费用户验证超过数量(50次/24小时\case 7: info = \商业用户不能通过验证\}

return info;

=

(4)MainFrame.java

package com.ui;

import java.awt.BorderLayout; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Label; import java.awt.TextArea; import java.awt.TextField;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import javax.swing.Box; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane;

import javax.swing.border.EmptyBorder;

12

import com.soap.WeatherServices;

public class MainFrame extends JFrame implements ActionListener { static String info=\ TextField tf1; Button bt1,bt2; Label label; JPanel contentPane; JScrollPane scrollPane; TextArea textArea; Box box1,box2; public MainFrame(String string){ super(string); setLayout(new FlowLayout());

label = new Label(\输入城市名:\label.setVisible(true);

tf1 = new TextField(5); tf1.setVisible(true); tf1.setEditable(true);

bt1=new Button(\查询\ bt1.addActionListener(this); bt1.setVisible(true); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5,5,5,5)); contentPane.setLayout(new BorderLayout(120,120)); this.setContentPane(contentPane); box1 = Box.createHorizontalBox(); box1.add(label); box1.add(tf1); box1.add(bt1); contentPane.add(box1,BorderLayout.NORTH); scrollPane=new JScrollPane(); scrollPane.setEnabled(true);

contentPane.add(scrollPane,BorderLayout.CENTER); textArea=new TextArea();

scrollPane.setViewportView(textArea); setBounds(500, 200, 400, 450); setVisible(true);

validate();//确保组件具有有效的布局 }

public void actionPerformed(ActionEvent e){ if(e.getSource()==bt1) { String city = tf1.getText().toString(); info=WeatherServices.getWeather(city); textArea.setText(info); } if (e.getID() == WindowEvent.WINDOW_CLOSING) {

13

}

System.exit(0); } }

public static void main(String[] args) { }

new MainFrame(\天气查询\

2.自定义服务(学生查询) (1)服务端

1)StudentInterfaceImp

package com.webservice.server; import javax.jws.WebService; import javax.xml.ws.Endpoint; /**

* @author 张波

* StudentInterfaceImp.java * 2015年5月28日 */

@WebService

public class StudentInterfaceImp implements StudentInterface{ String name[]={ \姜倩云\张丽梅\吴美庆\张忠敏\王雅蓉\葛红敏\赵斌灿\王严鑫\

\莫可宏\王猛\侯勇军\马铸辉\易鹏\罗路辉\闫治鹏\孙磊\李佳玲\\王勃博\康尧\张波\邓雷\陈佳鑫\唐炜宁\葛龙\倪赛杰\陈成\\刘浩浩\周子轩\宫蕊\文云丽\沈雪\刘冬媛\褚蓉\钱苑\董樟裕\\马凌杰\王京源\陈辉云\罗建兴\陈晨\赵文宇\宗玮雯\赵贝贝\

\黄豪琛\娄宽\任上海\王康\沈楠\蒋宇\吕阳\王刚\王扬\吕中剑\\张军\卢惺鑫\姚礼宗\

}; @Override public String studentQuery(int number) {

System.out.println(\学号:\ String result = name[number]; System.out.println(\姓名:\ return result; }

public static void main(String[] args) { Endpoint.publish(\/student\ System.out.println(\发布成功...\}

14

}

2)StudentInterfaceImp

package com.webservice.server; public interface StudentInterface { public String studentQuery(int number); }

(2)客户端(Client.java)

package com.webservice.client; import javax.swing.JFrame; import javax.swing.JPanel;

import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JTextArea; import javax.swing.Box;

import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Component; import java.awt.Color;

import com.webservice.server.StudentInterfaceImp;

import com.webservice.server.StudentInterfaceImpService; public class Client extends JFrame { private JPanel contentPane; private JTextField textField; private JButton search; private JButton cancel; private JTextArea textArea; private StudentInterfaceImpService impService; private StudentInterfaceImp imp; private static int number=0; public Client(String string) { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setForeground(Color.WHITE); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel label = new JLabel(\请输入学号进行查询\ label.setForeground(Color.BLUE); label.setBounds(155, 10, 129, 50);

15

contentPane.add(label); textField = new JTextField(\

textField.setBounds(155, 58, 110, 22); contentPane.add(textField); textField.setColumns(10);

// 通过webservice的服务视图

impService = new StudentInterfaceImpService(); // 获得服务端点

imp = impService.getStudentInterfaceImpPort();

search = new JButton(\查询\

search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource()==search) { number = Integer.parseInt(textField.getText().toString()); if(number>56||number<1){ result = \非法输入!\ textArea.setText(result); }else{

textArea.setText(\你输入的学号是:\查询结果为: \

System.out.println(result); } } }); search.setBounds(139, 104, 73, 23); contentPane.add(search); cancel = new JButton(\取消\ cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setText(\ textArea.setText(\ } }); cancel.setBounds(222, 104, 73, 23); contentPane.add(cancel); textArea = new JTextArea(); textArea.setBounds(105, 153, 223, 70); contentPane.add(textArea); Component glue = Box.createGlue(); glue.setBounds(61, 196, 1, 1); contentPane.add(glue); }

public static void main(String[] args) { Client frame = new Client(\自定义web service调用\ frame.setVisible(true);

16

}

}

17

本文来源:https://www.bwwdw.com/article/j36f.html

Top