您的位置:首页 > 其它

Web-Service-调用服务的方式

2014-05-14 20:58 375 查看

服务端

/**
*
*
* @author 姜沂
*
*         WebService 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
*
*/
@WebService
public class HelloService {
public static void main(String[] args) {
/**
* 静态的和final的方法 不能被发布 参数1:服务的发布地址 参数2:服务的实现者
*/
Endpoint.publish("http://127.0.0.1:9999/hello", new HelloService());
System.out.println("Server ready...");
}

public String sayHello(String name) {
System.err.println("----调用了------------");
return "hello" + name;
}
@WebMethod(exclude=true)//不暴露
public String sayHello2(String name) {
return "hello " + name;
}
}


客户端调用WebService的方式


l通过wsimport生成客户端代码


l通过客户端编程的方式调用

l使用javax.xml.ws.Service类用于访问web服务。
l关键类Service
•方法create – 用户创建Service对像,提供wsdlurl和服务名。
•getPort-用于通过指定namespace,portName和接口的范型。
•在客户端需要一个与服务器接口完全相同的类。(仍然使用工具生成。但只需要一个接口。并需要简单修改。如果返回的是复杂数据类型如POJO,还需要将POJO一并放到项目中)-不要试图通过-p来修改包名,会出错的。
l关键类QName – 被称为完全限定名即:Qualified Name的缩写。
lQName 的值包含名称空间 URI、本地部分和前缀。

public class Client {

/**
* 通过客户端编程的方式调用Webservice服务
* @throws MalformedURLException
*
*/
public static void main(String[] args) throws MalformedURLException {
//声明wsdl地址

URL wsdlUrl = new URL("http://127.0.0.1:9999/hello?wsdl");
//第一个参数是命名空间,第二个参数是一个完全限定名,WjService是服务名
Service s = Service.create(wsdlUrl, new QName("http://Service.jiangyi.com/","HelloServiceService"));
//通过service的getPort方法返回指定的接口
HelloService hs = s.getPort(new QName("http://Service.jiangyi.com/","HelloServicePort"), HelloService.class);
String ret = hs.sayHello("zhangsan");
//调用sayHello方法
System.out.println(ret);
}

}



l通过ajax调用js+XML 

使用原生的ajax调用web服务:

由于使用ajax – js调用web服务完成不同于使用java代码调用。所以,必须要对SOAP文件非常的了解。
一般使用ajax调用,应该是在已经获知了以下信息以后才去调用:
•获知请求(request)的soap文本。
•获知响应(response)的soap文本。

请求文件和响应文本格式,一般会随web服务的发布一同发布。

我们可以通过WSExplorer获取上面两段文本。

<html>
<head>
<title>通过ajax调用WebService服务</title>
<script>

function ajaxFunction(){
var xmlHttp;
try{ // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e){
try{// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}

return xmlHttp;
}
var xhr = ajaxFunction();
alert(xhr);
function sendMsg(){
var name = document.getElementById('name').value;
//服务的地址
var wsUrl = 'http://127.0.0.1:9999/hello';

//请求体
var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://Service.jiangyi.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
' <soapenv:Body> <q0:sayHello><arg0>'+name+'</arg0>  </q0:sayHello> </soapenv:Body> </soapenv:Envelope>';
//打开连接
xhr.open('POST',wsUrl,true);

//重新设置请求头
xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");

//设置回调函数
xhr.onreadystatechange = _back;

//发送请求
xhr.send(soap);
}

function _back(){
if(xhr.readyState == 4){
if(xhr.status == 200){
//alert('调用Webservice成功了');
var ret = xhr.responseXML;
var msg = ret.getElementsByTagName('return')[0];
document.getElementById('showInfo').innerHTML = msg.text;
//alert(msg.text);
}
}
}
</script>
</head>
<body>
<input type="button" value="发送SOAP请求" onclick="sendMsg();">
<input type="text" id="name">
<div id="showInfo">
</div>
</body>
</html>


以上代码中xml格式数据的生成方式












l通过URLConnection调用

Android端大部分用这中方式编程

/**
* 通过UrlConnection调用Webservice服务
*
*/
public class Client {
public static void main(String[] args) throws Exception {
//服务的地址
URL wsUrl = new URL("http://127.0.0.1:9999/hello");

HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");

OutputStream os = conn.getOutputStream();

//请求体
String soap	= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://Service.jiangyi.com/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+
"<soapenv:Body> <q0:sayHello><arg0>nice to meet you</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope";
os.write(soap.getBytes());

InputStream is = conn.getInputStream();

byte[] b = new byte[1024];
int len = 0;
String s = "";
while((len = is.read(b)) != -1){
String ss = new String(b,0,len,"UTF-8");
s += ss;
}
System.out.println(s);

is.close();
os.close();
conn.disconnect();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐