(封装xml)访问HTTP 接口2

来源:互联网 发布:linux破解root密码脚本 编辑:程序博客网 时间:2024/06/02 01:00

1.工具类XmlUtils:xml解析

import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.Node;/** * @author fanggt xml解析 */public class XmlUtils {// 文档private static Document doc = null;// 根节点private static Element root = null;public static void init(String xml) {// SAXReader reader = new SAXReader();try {doc = DocumentHelper.parseText(xml);root = doc.getRootElement();} catch (DocumentException e) {e.printStackTrace();}}/** * 获取名称为nodeName的文本 *  * @param nodeName * @return */public static String getNodeText(String nodeName) {Node n = root.selectSingleNode("//" + nodeName);return n.getText();}/** * 获取所有名称为nodeName的节点 *  * @param nodeName * @return */@SuppressWarnings("unchecked")public static List<Node> getNodeList(String nodeName) {return root.selectNodes("//" + nodeName);}/** * 获取节点名称为nodeName的attrName属性 *  * @param nodeName * @param attrName * @return */public static String getNodeAttribute(String nodeName, String attrName) {Node node = root.selectSingleNode("//" + nodeName);return node.valueOf("@" + attrName);}/** * 获取节点名称为nodeName的attrName属性 *  * @param nodeName * @param attrName * @return */@SuppressWarnings("unchecked")public static String getNodeAttribute(String nodeName, String attrName, int index) {List<Node> nodes = root.selectNodes("//" + nodeName + "[@" + attrName + "]");return nodes.get(index).valueOf("@" + attrName);}/** * 获取节点名称为nodeName的attrName属性 条件:改节点里面有个属性为key值为value的属性 *  * @param nodeName * @param attrName * @return */@SuppressWarnings("unchecked")public static String getNodeAttribute(String nodeName, String attrName, String key, String value) {List<Node> nodes = root.selectNodes("//" + nodeName + "[@" + key + "='" + value + "']");return nodes.get(0).valueOf("@" + attrName);}}

2.HttpUtils工具类

import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.methods.StringRequestEntity;import org.apache.commons.httpclient.params.HttpMethodParams;public class HttpUtils {    /** * 发送HTTP请求的API * @param url网址 * @param charset字符集 * @param paramsMap[String,Object] 请求参数集合 * @return返回访问网址后的HTTP响应结果文本 */public static String httpRequest(String url, String charset, String body) throws Exception {HttpClient client = new HttpClient();client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);PostMethod postMethod = null;try {postMethod = new PostMethod(url);RequestEntity requestEntity = new StringRequestEntity(body, "text/xml", "UTF-8");postMethod.setRequestEntity(requestEntity);int statusCode = client.executeMethod(postMethod);if (statusCode != HttpStatus.SC_OK) {String msg = "访问失败!!HTTP_STATUS=" + statusCode;throw new HttpException(msg);}// ifString context = postMethod.getResponseBodyAsString();return context;} finally {if (postMethod != null)postMethod.releaseConnection();}// finally}// method}




3.发送及接受xml格式的报文

String xml_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<MESSAGE>"+"<HEAD>"+"<TRX_CODE>TP1001</TRX_CODE>"+"<BUSINESS_CODE>"+mpd.getPayOrg()+"</BUSINESS_CODE>"+"<REQ_SN>" + reqSn +"</REQ_SN>"+"<TIMESTAMP>" + timeStamp + "</TIMESTAMP>"+"<SIGNED_MSG>";String xml_2 = "</SIGNED_MSG>"+"</HEAD>"+"<BODY>"+"<ACCOUNT_TYPE>" + mpd.getAccountType() + "</ACCOUNT_TYPE>"+"<BANK_NAME>" + mpd.getBankName() + "</BANK_NAME>"+"<ACCOUNT_NO>" + mpd.getAccountNo() + "</ACCOUNT_NO>"+"<ACCOUNT_NAME>" + mpd.getAccountName() + "</ACCOUNT_NAME>"+"<PROVINCE>" + mpd.getProvince() + "</PROVINCE>"+"<CITY>" + mpd.getCity() + "</CITY>"+"<CNAPS_NAME>" + mpd.getCnapsName() + "</CNAPS_NAME>"+"<CNAPS_NO>" + mpd.getCnapsNo() + "</CNAPS_NO>"+"<AMOUNT>" + amount + "</AMOUNT>"+"</BODY>"+"</MESSAGE>";// 进行签名的方法String signeddata = MD5.toMD5(xml_1 + xml_2 + MD5_KEY);//根据秘钥计算签名String body = xml_1 + signeddata + xml_2;System.out.println("--------------发送报文----------------------------");String res_xml = HttpUtils.httpRequest("http://192.168.**.**:8080/t/trans", "UTF-8", body);System.out.println("--------------接收报文----------------------------");Map<String, String> result = new HashMap<String, String>();XmlUtils.init(res_xml);reqSn = XmlUtils.getNodeText("REQ_SN");String respCode = XmlUtils.getNodeText("RESP_CODE");String respMsg = XmlUtils.getNodeText("RESP_MSG");result.put("respCode", respCode);result.put("respMsg", respMsg);result.put("reqSn", reqSn);return result;

原创粉丝点击