1.向soap接口发送soap报文
不知道如何调用别人的soap接口,试了好多框架的调用xf cxf都不行,网上看到一种httpclient很简单,试了下,果然成功了。
public class SoapService {
public String sendSoapMessage(String url, String message) throws Exception {
byte[] requestBytes;
requestBytes = message.getBytes("utf-8");
System.out.println("soap发送报文:"+message);
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length, "application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(requestEntity);
int stateCode = httpClient.executeMethod(postMethod);
String responseLine = "";
String soapResponseInfo = "";
if (stateCode == 200) {// 服务端返回正常
InputStream soapResponseStream = postMethod.getResponseBodyAsStream();
InputStreamReader inputStreamReader = new InputStreamReader(soapResponseStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while ((responseLine = bufferedReader.readLine()) != null) {
soapResponseInfo = soapResponseInfo + responseLine;
}
}
System.out.println("soap接收响应报文:"+soapResponseInfo);
return soapResponseInfo;
}
public static void main(String[] args) {
try {
SoapService soapService = new SoapService();
String url = "http://localhost:8081/tcb-java/webservice/erpWebService?wsdl";// wsdl文档的地址
String message ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.tcb.bms.sinoservices.com/\"><soapenv:Header/><soapenv:Body><web:invoke><arg0><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><receiveData><arReceiptBill><receiveMethod>TT</receiveMethod><receiptNo>171449762868-70958886</receiptNo><toAccountNo>3004800309024505201</toAccountNo><toAccountName></toAccountName><receiveDate>2018-08-20</receiveDate><exchangeRate>1</exchangeRate><currencyCode>CNY</currencyCode><amount>23760</amount><customerCode>10003379</customerCode><customerName>乌鲁木齐特变电工贸有限责任公司</customerName><remark>货款/货款</remark><receiptStatus>NEW</receiptStatus><businessType>2</businessType><companyCode>1009</companyCode></arReceiptBill></receiveData>]]></arg0></web:invoke></soapenv:Body></soapenv:Envelope>";
System.out.println(soapService.sendSoapMessage(url, message));
} catch (Exception e) {
e.printStackTrace();
}
}
}