本系统代码:
public void paynotify(){
String reqxml=util.createXMLxfb(resmap, list);
String resxml=Clientnew.httpParameterStream(url, reqxml);
}
// http发送报文方法
public class Clientnew {
public static String httpParameterStream(String surl,String parameter)throws Exception{
String str = "";
try{
System.out.println("webservie连接地址:"+surl);
HttpClient httpclient = new HttpClient(); // org.apache.commons.httpclient.HttpClient
PostMethod post = new PostMethod(surl); // org.apache.commons.httpclient.methods.PostMethod
String info = null;
RequestEntity entity = new StringRequestEntity(parameter, "text/xml", "utf-8"); // org.apache.commons.httpclient.methods.RequestEntity
post.setRequestHeader("xml", parameter);
post.setRequestEntity(entity);
httpclient.executeMethod(post);
int code = post.getStatusCode();
if (code == HttpStatus.SC_OK) {
System.out.println("连接webservice成功!");
info = new String(post.getResponseBodyAsString());
str = info;
}
post.releaseConnection();//释放连接
}catch(Exception e){
e.printStackTrace();
System.out.println("连接webservice异常!");
}
return str;
}
}
另一系统接收并发送应对报文:
public class PayAction extends BaseAction{
private String key="FRX_CSWEB_SIGNKEY";
String xml;
public String response(){
HttpServletRequest request=this.getRequest();
HttpServletResponse response=this.getResponse();
response.setCharacterEncoding("utf-8");
PrintWriter out = null;
MD5 md5=new MD5();
InputStream inputStream =null;
XMLUtil util = new XMLUtil();
try {
out = response.getWriter();
// 从request中取得输入流
inputStream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
xml = sb.toString();
System.out.println("xml======="+xml);
TreeMap hashmap = util.parseXMLString(xml);
buycard(hashmap,response);
} catch (Exception e) {
e.printStackTrace();
TreeMap<String, String> resmap = new TreeMap<String, String>();
resmap.put("TRANTYPE", "");
resmap.put("AGENTID", "");
resmap.put("CODE", "111");
resmap.put("DESC", "system error");
//对data进行加密
resmap.put("SIGN", createSign(resmap, key));
String resxml=util.createXMLxfb(resmap, null);
try {
out.print(resxml);
} catch (Exception e1) {
e1.printStackTrace();
}
}finally {
try {
inputStream.close();
if(out!=null){
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return SUCCESS;
}