码迷,mamicode.com
首页 > 编程语言 > 详细

用java http post模拟soapUI调用webservice

时间:2015-04-03 13:36:21      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:webservice   java   soapui   

工作中需要用java调用peoplesoft提供的webservice接口,但peoplesoft的实施方没有提供java调用的案例代码,soapUI可以调用,但java代码一直写不出来,自行学习并调通了用java http post模拟的方法,感谢天涯的zhouyun0243和谷歌!


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//需要commons-io的jar包
import org.apache.commons.io.IOUtils;
public class testwebservice {

public static void main(String[] args) {
try {
soapSpecialConnection();
} catch (Exception e) {e.printStackTrace();}}

public static void soapSpecialConnection() throws Exception{
String s = new String();
StringBuilder soapHeader = new StringBuilder();
//soapUI自动生成的request xml路径,写入传入参数
File file = new File("d:\\1.xml");
BufferedReader reader = null;
try {reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
soapHeader.append(tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {}
}
}
System.out.println("soapHeader="+soapHeader);
//设置soap请求报文的相关属性
//url从soapUI的request1的RAW标签的POST获取,url中不要有空格
String url="http://10.60.217.86:9527/dji-hrService/services/HrService.HrServiceHttpSoap11Endpoint/HTTP/1.1";
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
//Host,Content-Type,SOAPAction从soapUI的request1的RAW标签的Host,Content-Typ,SOAPActione获取
conn.setRequestProperty("Host", "10.60.217.86:9527");
conn.setRequestProperty("Content-Type", "ext/xml;charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(soapHeader.length()));
conn.setRequestProperty("SOAPAction", "urn:getWorkAttendanceByUidAndDate");
conn.setRequestMethod("POST");
//定义输出流
OutputStream output = conn.getOutputStream();
if (null != soapHeader) {
byte[] b = soapHeader.toString().getBytes("utf-8");
//发送soap请求报文
output.write(b, 0, b.length);}
output.flush();
output.close();
//定义输入流,获取soap响应报文
InputStream input = conn.getInputStream();
//需设置编码格式,否则会乱码
s=IOUtils.toString(input, "UTF-8");
input.close();
System.out.println("输出的xml="+s);}}
soapUI的request1的RAW标签示意

技术分享



用java http post模拟soapUI调用webservice

标签:webservice   java   soapui   

原文地址:http://blog.csdn.net/lihao2372/article/details/44852541

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!