apzl_leasing/src_cmb/com/tenwa/sdk/utils/HttpRequest.java
2018-06-19 19:56:54 +08:00

333 lines
11 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.tenwa.sdk.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import com.tenwa.sdk.VO.SDKCheckCallBackVO;
import com.tenwa.sdk.VO.SDKCheckCallDetail;
import com.tenwa.sdk.VO.SDKCheckCallInfoVO;
import com.tenwa.sdk.VO.SDKPayCallBackVO;
import com.tenwa.sdk.VO.SDKPayCallDetail;
import com.tenwa.sdk.VO.SDKPayCallInfoVO;
public class HttpRequest {
private String ipAddr = "http://192.168.7.85:65188"; // ²âÊÔµØÖ·¡£¡£¡£
private int timeout = 30 * 1000;
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public HttpRequest(String ipAddr) {
this.ipAddr = ipAddr;
}
public HttpRequest() {
}
public SDKPayCallBackVO sendPayRequest(String data) {
SDKPayCallBackVO callBack = new SDKPayCallBackVO();
SDKPayCallInfoVO info = new SDKPayCallInfoVO();
String result = "";
try {
URL url = new URL(this.ipAddr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(this.timeout);
connection.setReadTimeout(this.timeout);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Content-Type", "application/xml");
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "GBK");
out.append(data);
out.flush();
out.close();
int code = connection.getResponseCode();
InputStream is = null;
if (code == 200) {
is = connection.getInputStream();
} else {
is = connection.getErrorStream();
}
int length = (int) connection.getContentLength();
if (length != -1) {
byte[] datas = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, datas, destPos, readLen);
destPos += readLen;
}
result = new String(datas, "GBK");
}
is.close();
// System.out.println("----");
// System.out.println(result);
// System.out.println("----");
} catch (MalformedURLException e) {
info.setSTATUS(-1);
info.setERRMSG("URLЭÒé¡¢¸ñʽ»òÕß·¾¶´íÎó");
} catch (UnsupportedEncodingException e) {
info.setSTATUS(-1);
info.setERRMSG("×Ö·û±àÂëÓÐÎÊÌâ");
} catch (ProtocolException e) {
info.setSTATUS(-1);
info.setERRMSG("ЭÒéÒì³£");
} catch (IOException e) {
info.setSTATUS(-1);
info.setERRMSG("ÏìÓ¦³¬Ê±");
}
if (info.getSTATUS() != -1) {
info.setSTATUS(1);
info.setXML(result);
}
callBack.setInfo(info);
return toPayObj(callBack);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private SDKPayCallBackVO toPayObj(SDKPayCallBackVO vo) {
if (vo.getInfo().getSTATUS() != -1) {
SDKPayCallInfoVO info = vo.getInfo();
String result = vo.getInfo().getXML();
if (result != null && result.length() > 0) {
XmlPacket pktRsp = XmlPacket.valueOf(result);
if (pktRsp != null) {
String sRetCod = pktRsp.getRETCOD();
String sFunnam = pktRsp.getFUNNAM();
String sDattpy = pktRsp.getDATTYP();
String sErrmsg = pktRsp.getERRMSG();
info.setRETCOD(sRetCod);
info.setFUNNAM(sFunnam);
info.setDATTYP(sDattpy);
Vector<Map> vec = null ;
if (sRetCod.equals("0")) {
vec = pktRsp.getProperty("NTQPAYRQZ");
if(vec!=null && vec.size()>0) {
List<SDKPayCallDetail> details = new ArrayList<SDKPayCallDetail>();
for (Map propPayResult : vec) {
SDKPayCallDetail detail_one = new SDKPayCallDetail();
String REQSTS = (String) propPayResult.get("REQSTS");
String RTNFLG = (String) propPayResult.get("RTNFLG");
String SQRNBR = (String) propPayResult.get("SQRNBR");
String YURREF = (String) propPayResult.get("YURREF");
String REQNBR = (String) propPayResult.get("REQNBR");
String OPRSQN = (String) propPayResult.get("OPRSQN");
String OPRALS = (String) propPayResult.get("OPRALS");
String ERRCOD = (String) propPayResult.get("ERRCOD");
String ERRTXT = (String) propPayResult.get("ERRTXT");
detail_one.setERRCOD(ERRCOD);
detail_one.setERRTXT(ERRTXT);
detail_one.setOPRALS(OPRALS);
detail_one.setOPRSQN(OPRSQN);
detail_one.setREQNBR(REQNBR);
detail_one.setREQSTS(REQSTS);
detail_one.setRTNFLG(RTNFLG);
detail_one.setSQRNBR(SQRNBR);
detail_one.setYURREF(YURREF);
if (REQSTS.equals("FIN") && RTNFLG.equals("F")) {
detail_one.setERRTXT("Ö§¸¶Ê§°Ü£º" + ERRTXT);
} else {
detail_one.setERRTXT("Ö§¸¶Òѱ»ÒøÐÐÊÜÀí£¨Ö§¸¶×´Ì¬£º" + REQSTS + "£©");
}
details.add(detail_one);
}
vo.setDetails(details);
}
} else if (sRetCod.equals("-9")) {
info.setERRMSG("Ö§¸¶Î´ÖªÒì³££¬Çë²éѯ֧¸¶½á¹ûÈ·ÈÏÖ§¸¶×´Ì¬£¬´íÎóÐÅÏ¢£º" + sErrmsg);
} else {
info.setERRMSG("Ö§¸¶Ê§°Ü£º" + sErrmsg);
}
} else {
info.setERRMSG("ÏìÓ¦±¨ÎĽâÎöʧ°Ü");
}
}
vo.setInfo(info);
}
return vo;
}
public SDKCheckCallBackVO sendCheckRequest(String data) {
SDKCheckCallBackVO callBack = new SDKCheckCallBackVO();
SDKCheckCallInfoVO info = new SDKCheckCallInfoVO();
String result = "";
try {
URL url = new URL(this.ipAddr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(this.timeout);
connection.setReadTimeout(this.timeout);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Content-Type", "application/xml");
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "GBK");
out.append(data);
out.flush();
out.close();
int code = connection.getResponseCode();
InputStream is = null;
if (code == 200) {
is = connection.getInputStream();
} else {
is = connection.getErrorStream();
}
int length = (int) connection.getContentLength();
if (length != -1) {
byte[] datas = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, datas, destPos, readLen);
destPos += readLen;
}
result = new String(datas, "GBK");
}
is.close();
System.out.println("----");
System.out.println(result);
System.out.println("----");
} catch (MalformedURLException e) {
info.setSTATUS(-1);
info.setERRMSG("URLЭÒé¡¢¸ñʽ»òÕß·¾¶´íÎó");
} catch (UnsupportedEncodingException e) {
info.setSTATUS(-1);
info.setERRMSG("×Ö·û±àÂëÓÐÎÊÌâ");
} catch (ProtocolException e) {
info.setSTATUS(-1);
info.setERRMSG("ЭÒéÒì³£");
} catch (IOException e) {
info.setSTATUS(-1);
info.setERRMSG("ÏìÓ¦³¬Ê±");
}
if (info.getSTATUS() != -1) {
info.setSTATUS(1);
info.setXML(result);
}
callBack.setInfo(info);
return toCheckObj(callBack);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private SDKCheckCallBackVO toCheckObj(SDKCheckCallBackVO vo) {
if (vo.getInfo().getSTATUS() != -1) {
SDKCheckCallInfoVO info = vo.getInfo();
String result = vo.getInfo().getXML();
if (result != null && result.length() > 0) {
XmlPacket pktRsp = XmlPacket.valueOf(result);
if (pktRsp != null) {
String sRetCod = pktRsp.getRETCOD();
String sFunnam = pktRsp.getFUNNAM();
String sDattpy = pktRsp.getDATTYP();
String sErrmsg = pktRsp.getERRMSG();
info.setRETCOD(sRetCod);
info.setFUNNAM(sFunnam);
info.setDATTYP(sDattpy);
Vector<Map> vec = null ;
if (sRetCod.equals("0")) {
vec = pktRsp.getProperty("NTQPAYQYZ");
if(vec!=null && vec.size()>0) {
List<SDKCheckCallDetail> details = new ArrayList<SDKCheckCallDetail>();
for (Map propPayResult : vec) {
SDKCheckCallDetail detail_one = new SDKCheckCallDetail();
String BUSCOD = (String) propPayResult.get("BUSCOD");
String BUSMOD = (String) propPayResult.get("BUSMOD");
String DBTBBK = (String) propPayResult.get("DBTBBK");
String DBTACC = (String) propPayResult.get("DBTACC");
String DBTNAM = (String) propPayResult.get("DBTNAM");
String DBTBNK = (String) propPayResult.get("DBTBNK");
String CRTACC = (String) propPayResult.get("CRTACC");
String CRTNAM = (String) propPayResult.get("CRTNAM");
String CCYNBR = (String) propPayResult.get("CCYNBR");
String TRSAMT = (String) propPayResult.get("TRSAMT");
String YURREF = (String) propPayResult.get("YURREF");
String REQNBR = (String) propPayResult.get("REQNBR");
String REQSTS = (String) propPayResult.get("REQSTS");
String RTNFLG = (String) propPayResult.get("RTNFLG");
String RTNNAR = (String) propPayResult.get("RTNNAR");
detail_one.setBUSCOD(BUSCOD);
detail_one.setBUSMOD(BUSMOD);
detail_one.setDBTBBK(DBTBBK);
detail_one.setDBTACC(DBTACC);
detail_one.setDBTNAM(DBTNAM);
detail_one.setDBTBNK(DBTBNK);
detail_one.setCRTACC(CRTACC);
detail_one.setCRTNAM(CRTNAM);
detail_one.setCCYNBR(CCYNBR);
detail_one.setTRSAMT(TRSAMT);
detail_one.setYURREF(YURREF);
detail_one.setREQNBR(REQNBR);
detail_one.setYURREF(YURREF);
detail_one.setREQSTS(REQSTS);
detail_one.setRTNFLG(RTNFLG);
detail_one.setRTNNAR(RTNNAR);
details.add(detail_one);
}
vo.setDetails(details);
}
} else if (sRetCod.equals("-9")) {
info.setERRMSG("Ö§¸¶Î´ÖªÒì³££¬Çë²éѯ֧¸¶½á¹ûÈ·ÈÏÖ§¸¶×´Ì¬£¬´íÎóÐÅÏ¢£º" + sErrmsg);
} else {
info.setERRMSG("Ö§¸¶Ê§°Ü£º" + sErrmsg);
}
} else {
info.setERRMSG("ÏìÓ¦±¨ÎĽâÎöʧ°Ü");
}
}
vo.setInfo(info);
}
return vo;
}
}