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>