发送端:
HttpClient httpClient = new HttpClient(); //org.apache.commons.httpclient.HttpClient
PostMethod method = new PostMethod(connecturl);//org.apache.commons.httpclient.methods.PostMethod
method.setParameter("version", "1.0");
method.setParameter("service", "reapal.trust.onekeyRefund");
method.setParameter("partner", partner);
method.setParameter("sign", sign);
method.setParameter("signType", "0");
method.setParameter("reqData", reqData.toString());
//org.apache.commons.httpclient.params.HttpMethodParams //org.apache.commons.httpclient.DefaultHttpMethodRetryHandler
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
int statusCode = httpClient.executeMethod(method);
// 读取内容
byte[] responseBody = method.getResponseBody();
// 处理内容
if (method.getStatusCode() == 200) {
String response = new String(responseBody, "UTF-8");
............
} else {
return "01"; // 发送失败
}
接收端:
public String response(){
HttpServletRequest request=this.getRequest();
HttpServletResponse response=this.getResponse();
response.setCharacterEncoding("utf-8");
InputStream inputStream =null;
XMLUtil util = new XMLUtil();
try {
// 从request中取得输入流
inputStream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
xml = sb.toString();
TreeMap hashmap = util.parseXMLString(xml);
String sign=(String)hashmap.get("SIGN");
String trantype = (String) hashmap.get("TRANTYPE");
hashmap.remove("SIGN");
//============ 验签 ================
if (createSign(hashmap, key).equals(sign)) {
}
} catch (Exception e) {
e.printStackTrace();
TreeMap<String, String> resmap = new TreeMap<String, String>();
resmap.put("TRANTYPE", "");
resmap.put("ACCESSTYPE", "");
resmap.put("CODE", "04");
resmap.put("DESC", "系统异常");
//对data进行加密
resmap.put("SIGN", createSign(resmap, key));
String resxml=util.createXMLxfb(resmap, null);
try {
response.getWriter().print(resxml);
} catch (IOException e1) {
e1.printStackTrace();
}
}finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return SUCCESS;
}