Java发布一个简单 webservice应用 并发送SOAP请求
来源:互联网 发布:电影社交网络中的博客 编辑:程序博客网 时间:2024/06/17 04:50
首先搭建一个webservice的服务器,搭建成功后,运行项目,并访问 http://localhost:8080/service/SdicDataPort?wsdl“,得到如下wsdl文件,说明webservice发布成功:
<?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.6b21 svn-revision#12959. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.6b21 svn-revision#12959. --> - <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://sdic/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://sdic/" name="SdicDataService">- <types>- <xsd:schema> <xsd:import namespace="http://sdic/" schemaLocation="http://10.3.46.235:8080/service/SdicDataPort?xsd=1" /> </xsd:schema> </types>- <message name="getModifyRecords"> <part name="parameters" element="tns:getModifyRecords" /> </message>- <message name="getModifyRecordsResponse"> <part name="parameters" element="tns:getModifyRecordsResponse" /> </message>- <portType name="SdicDataDelegate">- <operation name="getModifyRecords"> <input wsam:Action="http://sdic/SdicDataDelegate/getModifyRecordsRequest" message="tns:getModifyRecords" /> <output wsam:Action="http://sdic/SdicDataDelegate/getModifyRecordsResponse" message="tns:getModifyRecordsResponse" /> </operation> </portType>- <binding name="SdicDataPortBinding" type="tns:SdicDataDelegate"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="getModifyRecords"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input>- <output> <soap:body use="literal" /> </output> </operation> </binding>- <service name="SdicDataService">- <port name="SdicDataPort" binding="tns:SdicDataPortBinding"> <soap:address location="http://10.3.46.235:8080/service/SdicDataPort" /> </port> </service> </definitions>
客户端访问webservice
通过 HttpClient 及 HttpURLConnection 发送SOAP请求,代码如下:
public String remoteGetDate(String endpoint,String starttime,String endtime) throws HttpException, IOException { int timeout = 60000; StringBuffer sb = new StringBuffer(""); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); sb.append("<soap:Envelope " + "xmlns:api='http://sdic/' " + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " + "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"); sb.append("<soap:Body>"); sb.append("<api:getModifyRecords>"); sb.append("<arg0>ls</arg0>"); sb.append("</api:getModifyRecords>"); sb.append("</soap:Body>"); sb.append("</soap:Envelope>"); try{ // HttpClient发送SOAP请求 System.out.println("HttpClient 发送SOAP请求"); System.out.println("参数输出:"+sb.toString()); HttpClient client = new HttpClient(); //设置用户名密码 //client.getState().setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("SDICEAMUSER", "SDICEAMPASSWORD")); PostMethod postMethod = new PostMethod(endpoint); // 设置连接超时 client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); // 设置读取时间超时 client.getHttpConnectionManager().getParams().setSoTimeout(timeout); // 然后把Soap请求数据添加到PostMethod中 RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8"); //设置请求头部,否则可能会报 “no SOAPAction header” 的错误 postMethod.setRequestHeader("SOAPAction",""); // 设置请求体 postMethod.setRequestEntity(requestEntity); int status = client.executeMethod(postMethod); // 打印请求状态码 System.out.println("status:" + status); // 获取响应体输入流 InputStream is = postMethod.getResponseBodyAsStream(); // 获取请求结果字符串 String result = IOUtils.toString(is); System.out.println("result: " + result); return result; }catch(ServiceLayerException se){ throw new ServiceLayerException("SOAP请求发送失败!"); } } // HttpURLConnection 发送SOAP请求(这个是转载的,需要修改下) System.out.println("HttpURLConnection 发送SOAP请求"); URL url = new URL(wsdl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestMethod("POST"); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.write(sb.toString().getBytes("utf-8")); dos.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String line = null; StringBuffer strBuf = new StringBuffer(); while ((line = reader.readLine()) != null) { strBuf.append(line); } dos.close(); reader.close(); System.out.println(strBuf.toString()); }
响应报文如下:
<?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:sayHelloResponse xmlns:ns2="http://demo.ls.com/"> <return>************</return> </ns2:sayHelloResponse> </S:Body> </S:Envelope>
具体的数据是之中的内容,需要通过xml方式截取
阅读全文
0 0
- Java发布一个简单 webservice应用 并发送SOAP请求
- cxf 发送 soap 请求(webService)
- jmeter发送soap请求-webservice接口
- java使用POST发送soap报文请求webservice返回500错误解析
- 创建一个简单的webservice服务并发布
- PHP soap 调用 JAVA发布的webservice
- java 请求响应soap(webService)
- java webservice soap消息请求例子
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- jmeter发送soap请求进行webservice接口压力测试
- Jmeter发送SOAP请求对WebService接口测试
- Jmeter发送SOAP请求对WebService接口压力测试
- Jmeter3.2版本发送SOAP请求测试webservice接口
- Jmeter3.2版本发送SOAP请求测试webservice接口
- 抓取Webservice Soap请求
- Android Soap请求webService
- 高精度加法
- C代码调用汇编&使用指令集优化——x264汇编代码原理
- J2SE基础面经
- sqlserver、Mysql、Oracle三种数据库的优缺点总结
- 机器学习 28 个视频教程
- Java发布一个简单 webservice应用 并发送SOAP请求
- 《Python自然语言处理》学习笔记-第五章
- 将xgboost0.4升级到xgboost0.6版本
- OpenJudge百炼-2706-麦森数-C语言-高精度计算
- uefi bios debug 终级方案 反汇编
- Git命令
- [设计模式]单例模式
- 【Unity】【C#】【Code】List示例代码
- Intersection of Two Arrays II--求交集