@Override
public net.sf.json.JSONObject proSalePrice(String url, QueryListDto queryDto) throws Exception {
// String url = "http://10.1.225.155:80/SPH_MOBILE_ERP/salePrice/ProxyServices/selectSalePrice_PS?wsdl";
net.sf.json.JSONObject rt = null;
//第一步:创建服务地址,不是WSDL地址
URL path = new URL(url);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) path.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数据,发送请求
//InputStream in = this.getClass().getResourceAsStream("salePrice.xml");//本地测试可以
InputStream in1 = ProductServiceImpl.class.getClassLoader().getResourceAsStream("./webService/salePrice.xml");//本地测试可以 ,服务器不可以
InputStream in2 = ProductServiceImpl.class.getClassLoader().getResourceAsStream("/webService/salePrice.xml"); //本地测试不可以 ,服务器可以,通过CLASSPATH读取包内文件,注意以“/”开头
InputStream in = new ClassPathResource("webService/salePrice.xml").getInputStream();//本地测试可以 ,服务器可以
System.out.println("ProductServiceImpl.class.getClassLoader().getResourceAsStream(\"./webService/salePrice.xml\") :"+in1);
System.out.println("ProductServiceImpl.class.getClassLoader().getResourceAsStream(\"/webService/salePrice.xml\") :"+in2);
System.out.println("new ClassPathResource(\"webService/salePrice.xml\").getInputStream() :"+in);
//ServletContext().getRealPath("路径名A");好像这里不可以用此方法
String path2 = ProductServiceImpl.class.getClassLoader().getResource("").getPath();
System.out.println("ProductServiceImpl.class.getClassLoader().getResource(\"\").getPath():"+path);
// license.xml应放在..\WebRoot\WEB-INF\classes路径下
// InputStream is = Convert2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
byte[] data = this.toByteArray(in);
System.out.println("this.toByteArray(in): "+data);
String soapXML = new String(data);
System.out.println("proSalePrice请求头:"+ soapXML);
Map<String, String> param_map = queryDto.getAttr();
String al_party_id = param_map.get("al_party_id");
String al_goods_id = param_map.get("al_goods_id");
String al_client_id = param_map.get("al_client_id");
String al_payment_day = param_map.get("al_payment_day");
String dbSource = param_map.get("dbSource");
soapXML = soapXML.replaceAll("#al_party_id", al_party_id)
.replaceAll("#al_goods_id", al_goods_id)
.replaceAll("#al_client_id", al_client_id)
.replaceAll("#al_payment_day", al_payment_day)
.replaceAll("#dbSource", dbSource);
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
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);
}
String resultStr = sb.toString();
System.out.println(resultStr);
JSONObject mapResult = this.xml2Json(resultStr);
rt = net.sf.json.JSONObject.fromObject(mapResult);
is.close();
isr.close();
br.close();
System.out.println(rt);
return rt;
}
os.close();
return null;
}
配置的xml(传输数据)
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://www.shaphar.com/SoapService">
<soapenv:Header/>
<soapenv:Body>
<soap:REQUEST_DATA>
<soap:commonHeader>
<soap:BIZTRANSACTIONID>?</soap:BIZTRANSACTIONID>
<soap:COUNT>?</soap:COUNT>
<soap:CONSUMER>?</soap:CONSUMER>
<soap:SRVLEVEL>?</soap:SRVLEVEL>
<soap:ACCOUNT>?</soap:ACCOUNT>
<soap:PASSWORD>?</soap:PASSWORD>
<soap:COMMENTS>?</soap:COMMENTS>
</soap:commonHeader>
<soap:LIST>
<soap:AL_PARTY_ID>#al_party_id</soap:AL_PARTY_ID>
<soap:AL_GOODS_ID>#al_goods_id</soap:AL_GOODS_ID>
<soap:AL_CLIENT_ID>#al_client_id</soap:AL_CLIENT_ID>
<soap:AL_PAYMENT_DAY>#al_payment_day</soap:AL_PAYMENT_DAY>
<soap:transSource>#dbSource</soap:transSource>
</soap:LIST>
</soap:REQUEST_DATA>
</soapenv:Body>
</soapenv:Envelope>