-
服务端
代码如下,控制台打印”ServiceTestStart”,浏览器访问http://localhost:8080/MyService?wsdl,出现xml标志成功。
package webservce;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class ServceTest { public String getMessage(String name) { return name+"你好"; } public static void main(String[] args) { Endpoint.publish("http://localhost:8080/MyService",new ServceTest());//发布服务 System.out.println("ServiceTestStart"); } }
-
客户端
E:\eclipse\workspace\Advance\src 为项目路径,webservceclient 为项目下的包。cmd执行命令后,客户端相关java代码生成在E:\eclipse\workspace\Advance\src\webservceclient目录下。特别注意执行该命令把服务起来,否则会报 failed.noservice=在提供的 WSDL 中找不到 wsdl:service:。出现正在生成代码…正在编译代码…成功。
wsimport -s E:\\eclipse\\workspace\\Advance\\src -p webservceclient -keep http://localhost:8080/MyService?wsdl
-
测试
我测试和服务端写同一目录下了,注意ServceTest是接口。打印出”sinaihalo你好”,测试成功。
package webservce;
import webservceclient.ServceTestService;
public class ClientTest { public static void main(String[] args) { webservceclient.ServceTest serviceTest = new ServceTestService().getServceTestPort();//初始化对象 String name = serviceTest.getMessage("sinaihalo");//调用服务端方法 System.out.println(name);//打印返回结果 } }
-
httpclient方式测试
首先导httpclient的jar包,这种方式自己拼请求,运行结果:
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:getMessageResponse xmlns:ns2="http://webservce/"><return>sinaihalo你好</return></ns2:getMessageResponse></S:Body></S:Envelope>
package com.ultrapower.nettech.obm.server;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class TestWebService { public static void main(String[] args) throws Exception { Map<String,String> map = new HashMap<String,String>(); //拼接xml请求,带有请求头 String soapRequestData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservce/\">"
+"<soapenv:Header/>"
+"<soapenv:Body>"
+"<web:getMessage>"
+" <!--Optional:-->"
+"<arg0>sinaihalo</arg0>"
+"</web:getMessage>"
+"</soapenv:Body>"
+"</soapenv:Envelope>";
try {
String method = "http://localhost:8080/MyService";//比如http://192.177.222.222:8888/services/Service_Name/Function_Name
PostMethod postMethod = new PostMethod(method);
byte[] b = soapRequestData.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b,0,b.length);
RequestEntity re = new InputStreamRequestEntity(is,b.length,"text/xml; charset=utf-8");
postMethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
//200说明正常返回数据
if (statusCode != 200) {
//internet error
System.out.println(statusCode);
}
soapRequestData = postMethod.getResponseBodyAsString();
System.out.println(soapRequestData);
} catch (Exception e) {
e.printStackTrace();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。