向soap接口发送soap报文

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();
        }
    }

}

 

 

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值