getElementValue

/**
* 解析返回的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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值