/**
* 解析返回的xml格式字符串中是否包含指定的元素
* 包含则返回值,否则返回空字符串
* @param result
* @param element
* @return
*/
@SuppressWarnings("deprecation")
public static String getValue(String result, String element) {
String str = "";
if (StringUtils.isNotEmpty(result) && StringUtils.isNotEmpty(element) && result.contains(element)) {
try {
int elementIndex = result.indexOf(element);
int middleIndex = result.indexOf("&");
if (elementIndex > middleIndex) {
str = result.substring(elementIndex + element.length() + 1, result.length());
} else {
str = result.substring(elementIndex + element.length() + 1, middleIndex);
}
return java.net.URLDecoder.decode(str);
} catch (Exception e) {
log.info("解析数据时未找到相关元素:" + element);
}
}
return str;
}
public static void main(String[] args) {
String str = "Plain=TranAbbr%3DIPER%7CAcqSsn%3D000012307996%7CMercDtTm%3D20160620143930%7CTermSsn%3D12434509%7CRespCode%3D00%7CTermCode%3D00000000%7CMercCode%3D983708160001401%7CTranAmt%3D0.01%7CSettDate%3D20160620&Signature=a12132qq";
String str1 = getValue(str, "Plain");
System.out.println("Plain ===" + str1);
String str2 = getValue(str, "Signature");
System.out.println("Signature ===" + str2);
String str3 = getElementValue(str,"Plain");
System.out.println("Plain = " + str3);
String str4 = getElementValue(str,"Signature");
System.out.println("Signature = " + str4);
}
@SuppressWarnings("deprecation")
private static String getElementValue(String str, String string) {
Map<String,String> map = new HashMap<String, String>();
if (StringUtils.isNotEmpty(str) && StringUtils.isNotEmpty(string) && StringUtils.contains(str, string)) {
String [] inputStr = StringUtils.split(str,"&");
for(int i = 0; i< inputStr.length; i++){
String key = inputStr[i].split("=")[0];
String value = inputStr[i].split("=")[1];
map.put(key, value);
}
}
return map.size() > 0 ? java.net.URLDecoder.decode(map.get(string)) : "";
}
@SuppressWarnings("deprecation")
private static Map<String, String> getElementToMap(String str) {
Map<String, String> map = new HashMap<String, String>();
if (StringUtils.isNotEmpty(str) && str.contains("&")) {
String[] inputArr = StringUtils.split(str, "&");
for (int i = 0; i < inputArr.length; i++) {
String key = inputArr[i].split("=")[0];
String value = inputArr[i].split("=")[1];
map.put(key, value);
}
}
return map.size() > 0 ? map : new HashMap<String, String>();
}
public static String sendMessage(String message) throws IOException {
// 对报文进行URLENcoder编码
message = URLEncoder.encode(message,"utf-8");
OutputStreamWriter osw = null;
HttpURLConnection httpConn = null;
BufferedReader br = null;
StringBuilder buff = new StringBuilder();
try {
URL url = new URL(BankMessageUtil.ChinapayMobilePayUrl);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-length", "" + message.getBytes().length);
httpConn.setRequestProperty("Content-Type", "application/octet-stream");
httpConn.setRequestProperty("Charset", "utf-8");
httpConn.setConnectTimeout(10000);
httpConn.setReadTimeout(30000);
// 写入信息
osw = new OutputStreamWriter(httpConn.getOutputStream());
osw.write(message);
osw.flush();
// 读取返回结果
String line = null;
br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while((line = br.readLine()) != null) {
buff.append(line);
}
} catch(Exception e) {
log.error("ChinaPayMobileUtil exception", e);
} finally {
if (osw != null) {
osw.close();
}
if (br != null) {
br.close();
}
}
// URLDecoder解码
String result = URLDecoder.decode(buff.toString(), "utf-8").replaceAll("\r|\n|\t", "");
return result;
}
* 解析返回的xml格式字符串中是否包含指定的元素
* 包含则返回值,否则返回空字符串
* @param result
* @param element
* @return
*/
@SuppressWarnings("deprecation")
public static String getValue(String result, String element) {
String str = "";
if (StringUtils.isNotEmpty(result) && StringUtils.isNotEmpty(element) && result.contains(element)) {
try {
int elementIndex = result.indexOf(element);
int middleIndex = result.indexOf("&");
if (elementIndex > middleIndex) {
str = result.substring(elementIndex + element.length() + 1, result.length());
} else {
str = result.substring(elementIndex + element.length() + 1, middleIndex);
}
return java.net.URLDecoder.decode(str);
} catch (Exception e) {
log.info("解析数据时未找到相关元素:" + element);
}
}
return str;
}
public static void main(String[] args) {
String str = "Plain=TranAbbr%3DIPER%7CAcqSsn%3D000012307996%7CMercDtTm%3D20160620143930%7CTermSsn%3D12434509%7CRespCode%3D00%7CTermCode%3D00000000%7CMercCode%3D983708160001401%7CTranAmt%3D0.01%7CSettDate%3D20160620&Signature=a12132qq";
String str1 = getValue(str, "Plain");
System.out.println("Plain ===" + str1);
String str2 = getValue(str, "Signature");
System.out.println("Signature ===" + str2);
String str3 = getElementValue(str,"Plain");
System.out.println("Plain = " + str3);
String str4 = getElementValue(str,"Signature");
System.out.println("Signature = " + str4);
}
@SuppressWarnings("deprecation")
private static String getElementValue(String str, String string) {
Map<String,String> map = new HashMap<String, String>();
if (StringUtils.isNotEmpty(str) && StringUtils.isNotEmpty(string) && StringUtils.contains(str, string)) {
String [] inputStr = StringUtils.split(str,"&");
for(int i = 0; i< inputStr.length; i++){
String key = inputStr[i].split("=")[0];
String value = inputStr[i].split("=")[1];
map.put(key, value);
}
}
return map.size() > 0 ? java.net.URLDecoder.decode(map.get(string)) : "";
}
@SuppressWarnings("deprecation")
private static Map<String, String> getElementToMap(String str) {
Map<String, String> map = new HashMap<String, String>();
if (StringUtils.isNotEmpty(str) && str.contains("&")) {
String[] inputArr = StringUtils.split(str, "&");
for (int i = 0; i < inputArr.length; i++) {
String key = inputArr[i].split("=")[0];
String value = inputArr[i].split("=")[1];
map.put(key, value);
}
}
return map.size() > 0 ? map : new HashMap<String, String>();
}
public static String sendMessage(String message) throws IOException {
// 对报文进行URLENcoder编码
message = URLEncoder.encode(message,"utf-8");
OutputStreamWriter osw = null;
HttpURLConnection httpConn = null;
BufferedReader br = null;
StringBuilder buff = new StringBuilder();
try {
URL url = new URL(BankMessageUtil.ChinapayMobilePayUrl);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-length", "" + message.getBytes().length);
httpConn.setRequestProperty("Content-Type", "application/octet-stream");
httpConn.setRequestProperty("Charset", "utf-8");
httpConn.setConnectTimeout(10000);
httpConn.setReadTimeout(30000);
// 写入信息
osw = new OutputStreamWriter(httpConn.getOutputStream());
osw.write(message);
osw.flush();
// 读取返回结果
String line = null;
br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while((line = br.readLine()) != null) {
buff.append(line);
}
} catch(Exception e) {
log.error("ChinaPayMobileUtil exception", e);
} finally {
if (osw != null) {
osw.close();
}
if (br != null) {
br.close();
}
}
// URLDecoder解码
String result = URLDecoder.decode(buff.toString(), "utf-8").replaceAll("\r|\n|\t", "");
return result;
}