----------服务端---------
xml 配置
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="userOrg"
implementor="com.hotent.mini.ext.service.impl.UserOrgServiceWsdlImpl"
address="/userOrg">
</jaxws:endpoint>
// 实现类
package com.hotent.mini.ext.service.impl;
public class UserOrgServiceWsdlImpl implements IUserOrgWsdlService {
@Override
public String getUserList(String xml) {
return xml;
}
}
// 接口
package com.hotent.mini.ext.service;
@WebService(serviceName="serviceUserOrg", endpointInterface = "com.hotent.mini.ext.service.IUserOrgWsdlService",
targetNamespace = "http://impl.webservice.platform.hotent.com/")
public interface IUserOrgWsdlService {
String getUserList(String xml);
}
---------客户端--------
public static void main(String args[]) {
// 第一种方法 ----------------------------------------------
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
// 创建客户端连接
Client client = factory.createClient("http://127.0.0.1:8080/xx/service/userOrg?wsdl");
Object[] res = null;
try {
QName operationName = new QName("http://impl.webservice.userorg.com/","findUser");//如果有命名空间需要加上这个,第一个参数为命名空间名称,调用的方法名称
res = client.invoke(operationName, "admin");//后面为WebService请求参数数组
System.out.println(res[0]);
}catch (Exception e) {
e.printStackTrace();
}
// 第二种方法 ----------------------------------------------
// 被<![CDATA[]]>这个标记所包含的内容将表示为纯文本
String xmlData = "<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<accounts>" +
"<account>" +
"<accId>帐号ID(必填)</accId>" +
"<userPasswordMD5>密码</userPasswordMD5>" +
"<userPasswordSHA1>密码</userPasswordSHA1>" +
"其中userPasswordSHA1标签代表SHA1加密后的密码,userPasswordMD5标签代表MD5加密后的密码" +
"<name>姓名</name>" +
"<sn>姓</sn>" +
"<description>描述 </description>" +
"<email>邮箱 </email>" +
"<gender>性别</gender>" +
"<telephoneNumber>电话号码</telephoneNumber>" +
"<mobile>移动电话</mobile>" +
"<startTime>用户的开始生效时间(YYYY-MM-DD HH:mm:SS)</startTime>" +
"<endTime>用户结束生效时间(YYYY-MM-DD HH:mm:SS) </endTime>" +
"<idCardNumber>身份证号码</idCardNumber>" +
"<employeeNumber>工号 </employeeNumber>" +
"<o>用户所属的组织的编码号 </o>" +
"<employeeType>用户类型</employeeType>" +
"<supporterCorpName>所在公司名称 </supporterCorpName>" +
"</account>" +
"</accounts>]]>";
//调用方法
String method = "sayHello";
method = "getUserList";
String data="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://impl.webservice.platform.hotent.com/\">"+
"<soapenv:Body>"+
"<ser:"+method+">"+
"<arg0>"+ xmlData + "</arg0>"+
"</ser:"+method+">"+
"</soapenv:Body>"+
"</soapenv:Envelope>";
String httpUrl="http://127.0.0.1:8080/xx/service/helloWorld?wsdl";
httpUrl="http://127.0.0.1:8080/xx/service/userOrg?wsdl";
try {
//第一步:创建服务地址
URL url = new URL(httpUrl);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
String soapXML = data;
//将信息以流的方式发送出去
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
System.out.println("responseCode: "+responseCode);
if(200 == responseCode){//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
is.close();
isr.close();
br.close();
System.out.println(StringEscapeUtils.unescapeXml(sb.toString())); //转义
System.out.println(sb.toString());
} else { //异常信息
InputStream is = connection.getErrorStream(); //通过getErrorStream了解错误的详情,因为错误详情也以XML格式返回,因此也可以用JDOM来获取。
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader in = new BufferedReader(isr);
String inputLine;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("d:\\result.xml")));// 将结果存放的位置
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
bw.write(inputLine);
bw.newLine();
bw.close();
}
in.close();
}
os.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 把xml转义
public static String escapeXml(String xml) {
String newXml = xml.replaceAll("<", "<").replaceAll(">", ">").replaceAll(" ", " ").replaceAll("\"", """);
return newXml;
}