Java调用C#带soapheader的webservice接口

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

import sun.misc.BASE64Encoder;

 

/**
  *
  * 功能描述:创建xml文件
  * 时间:2013-7-24
  * @author:
  */
 public static String createXmlSendABCOfXj(OrgAuthHeader oah){
  String xmlfile="<?xml version='1.0' encoding='utf-8'?><soap:Envelope 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/'>";
  if(oah!=null){
   xmlfile+="<soap:Header><OrgAuthHeader xmlns='http://tempuri.org/'>";
   xmlfile+="<OrganNumber>"+(oah.getOrganNumber()==null?"":oah.getOrganNumber())+"</OrganNumber>";
   xmlfile+="<OrganSerial>"+(oah.getOrganSerial()==null?"":oah.getOrganSerial())+"</OrganSerial>";
   xmlfile+="<TerminalID>"+(oah.getTerminalID()==null?"":oah.getTerminalID())+"</TerminalID>";
   xmlfile+="<KeyDate>"+(oah.getKeyDate()==null?"":oah.getKeyDate())+"</KeyDate>";
   xmlfile+="<NewKey>"+(oah.getNewKey()==null?new BASE64Encoder().encode("".getBytes()):new BASE64Encoder().encode(oah.getNewKey().getBytes()))+"</NewKey>";
   xmlfile+="</OrgAuthHeader></soap:Header><soap:Body><BankTranCall xmlns='http://tempuri.org/'>";
   xmlfile+="<trancode>"+(oah.getTrancode()==null?"":oah.getTrancode())+"</trancode>";
   xmlfile+="<recvthirdpack>"+(oah.getRecvthirdpack()==null?"":oah.getRecvthirdpack())+"</recvthirdpack>";
   xmlfile+="<oprator>"+(oah.getOprator()==null?"":oah.getOprator())+"</oprator>";
   xmlfile+="</BankTranCall></soap:Body>";
  }
  xmlfile+="</soap:Envelope>";
  return xmlfile;
 }
 
 /**
  *
  * 功能描述:创建交易内容xml文件
  * 时间:2013-7-24
  * @author:
  */
 public static String createXmlTrans(TransAccDetailInfo tadi){
  String xmlfile="<?xml version='1.0' encoding='GBK' standalone='yes'?><input>";
  if(tadi!=null){
   xmlfile+="<ReqSeqNo>"+(tadi.getReqSeqNo()==null?"":tadi.getReqSeqNo())+"</ReqSeqNo>";
   xmlfile+="<OrganNumber>"+(tadi.getOrganNumber()==null?"":tadi.getOrganNumber())+"</OrganNumber>";
   xmlfile+="<cardno>"+(tadi.getCardno()==null?"":tadi.getCardno())+"</cardno>";
   xmlfile+="<password>"+(tadi.getPassword()==null?"":tadi.getPassword())+"</password>";
   xmlfile+="<amount>"+(String.valueOf(tadi.getAmount())==null?"":tadi.getAmount())+"</amount>";
   xmlfile+="<track2>"+(tadi.getTrack2()==null?"":tadi.getTrack2())+"</track2>";
   xmlfile+="<track3>"+(tadi.getTrack3()==null?"":tadi.getTrack3())+"</track3>";
   xmlfile+="<logno>"+(tadi.getLogno()==null?"":tadi.getLogno())+"</logno>";
   xmlfile+="<vchno>"+(tadi.getVchno()==null?"":tadi.getVchno())+"</vchno>";
   xmlfile+="<begindate>"+(tadi.getBegindate()==null?"":tadi.getBegindate())+"</begindate>";
   xmlfile+="<enddate>"+(tadi.getEnddate()==null?"":tadi.getEnddate())+"</enddate>";
  }
  xmlfile+="</input>";
  return xmlfile;
 }
 /**
  *
  * 功能描述:发送xml文件以及接受返回回来的消息和返回发送的报文体
  * 时间:2013-7-24
  * @author:
  */
 public static String sendMsg(OrgAuthHeader oah)throws Exception{
  String xmlFile=createXmlSendABCOfXj(oah);//获取发送的xml文件
  URL url = new URL(xjabc_url);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestProperty("Content-Length", String.valueOf(xmlFile.getBytes().length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("soapActionString", SOAPAction);
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        OutputStream out = httpConn.getOutputStream();
        out.write(xmlFile.getBytes());
        out.close();
        byte[] datas=readInputStream(httpConn.getInputStream());
        String result=new String(datas);
        System.out.println("2222222222222222:"+result);
        String msg=result+"@"+xmlFile;
  return msg;
 }
   
    /**
     * 从输入流中读取数据
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len = inStream.read(buffer)) !=-1 ){
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();//网页的二进制数据
        outStream.close();
        inStream.close();
        return data;
    }

<xjabc_SOAPAction>http://tempuri.org/BankTranCall</xjabc_SOAPAction>

 

接口:

POST/WebSite/WebService/UniInterface.asmx HTTP/1.1

Host:192.168.192.13

Content-Type:text/xml; charset=utf-8

Content-Length:length

SOAPAction:"http://tempuri.org/BankTranCall"

 

<?xmlversion="1.0" encoding="utf-8"?>

<soap:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Header>

    <OrgAuthHeaderxmlns="http://tempuri.org/">

      <OrganNumber>string</OrganNumber>

      <OrganSerial>base64Binary</OrganSerial>

      <KeyDate>string</KeyDate>

      <NewKey>base64Binary</NewKey>

    </OrgAuthHeader>

  </soap:Header>

  <soap:Body>

    <BankTranCallxmlns="http://tempuri.org/">

      <trancode>string</trancode>

      <recvthirdpack>base64Binary</recvthirdpack>

    </BankTranCall>

  </soap:Body>

</soap:Envelope>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxl333

原创不容易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值