1,316
社区成员
发帖
与我相关
我的任务
分享
报文格式:
private String getLoginRequest(String AccountCode, String Passwd)
{
String req = new String();
req =
"<!xml version=\"1.0\" encoding=\"GB2312\" ?><transaction><pub><transcode>8001</transcode><applno>"
+ appno
+ "</applno><ver>1</ver></pub>"
+ "<parin><login loginid=\""
+ AccountCode
+ "\" password=\""
+ Passwd
+ "\"/></parin></transaction>";
return req;
}
通信方式:
public Communicate(String msg) throws java.io.IOException,Exception{
String urlName="HTTP://129.15.132.1/MY/rbac/servlet/MY.rbac.interfaced.InterfaceToBS";//测试
URL url = new URL(urlName);
URLConnection connection = url.openConnection();//建立连接
String strMsg = URLEncoder.encode(msg);
connection.setDoOutput(true);
connection.setDoInput(true);
try
{
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.print("msg=" + strMsg + "\n");
out.close();
} catch (IOException ex)
{
throw ex;
}
String line;
String strRet;
String retCode, errMsg;
try
{ // b 接收
BufferedReader in =
new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO8859_1"));
for (strRet = "";(line = in.readLine()) != null; strRet = strRet + line);
in.close();
} catch (IOException ex)
{
throw ex;
}
if (strRet == null || strRet.equals(""))
{
throw new Exception("认证系统返回信息NULL");
}
String aa = "";
try
{
aa = URLDecoder.decode(strRet);
this.setMsg(aa);
} catch (Exception e)
{
throw e;
}
}
如何通信:
/**
* 用于柜员登录交易
*
*/
public UserContainer Login(String AccountCode, String Passwd) throws SystemException
{
try{
Document doc = execute(getLoginRequest(AccountCode,Passwd));
return loginPaser(doc);
}catch(SystemException e)
{
throw e;
}catch(Exception e)
{
throw new SystemException("RbacTool.Login()","ERR-00002",e.toString());
}
}
private Document execute(String request)throws Exception
{
Communicate comm = new Communicate(request);
return preParser(comm.getMsg());
}
//转化为Document格式
private Document preParser(String text) throws Exception{
//System.out.println(text);
//DOMParser parser = new DOMParser();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new java.io.StringReader(text));
Document doc = XmlTools.getInctance().getDocument(inputSource);
/*try {
parser.parse(inputSource);
} catch (org.xml.sax.SAXException e) {
System.out.println(e);
} catch (java.io.IOException e) {
System.out.println(e.toString());
} catch (Exception e) {
System.out.println(e.toString());
}*/
return doc;
//return parser.getDocument();
}
解析报文:
private UserContainer loginPaser(Document doc)throws SystemException
{
NodeList nodes;
nodes = doc.getElementsByTagName("code");
String code = nodes.item(0).getFirstChild().getNodeValue();
nodes = doc.getElementsByTagName("message");
String message = nodes.item(0).getFirstChild().getNodeValue();
if (code.equals("1"))
{
nodes = doc.getElementsByTagName("employee");
UserContainer uc = new UserContainer();
uc.setUserId(nodes.item(0).getAttributes().getNamedItem("employeeid").getNodeValue());
uc.setUserName(nodes.item(0).getAttributes().getNamedItem("chnname").getNodeValue());
uc.setAlias(nodes.item(0).getAttributes().getNamedItem("alias").getNodeValue());
uc.setDepart(nodes.item(0).getAttributes().getNamedItem("department").getNodeValue());
uc.setSection(nodes.item(0).getAttributes().getNamedItem("section").getNodeValue());
uc.setVirtualinst(nodes.item(0).getAttributes().getNamedItem("virtualinst").getNodeValue());
uc.setVirtualinstname(nodes.item(0).getAttributes().getNamedItem("virtualinstname").getNodeValue());
nodes = doc.getElementsByTagName("organization");
Organization org = new Organization();
org.setInstCode(nodes.item(0).getAttributes().getNamedItem("institution").getNodeValue());
org.setInstName(nodes.item(0).getAttributes().getNamedItem("instname").getNodeValue());
org.setBelongTo(nodes.item(0).getAttributes().getNamedItem("belongto").getNodeValue());
org.setHostCode(nodes.item(0).getAttributes().getNamedItem("siteonhost").getNodeValue());
org.setBankFlag(nodes.item(0).getAttributes().getNamedItem("bankflag").getNodeValue());
uc.setOrgan(org);
nodes = doc.getElementsByTagName("roles");
Node roleNode = nodes.item(0);
Roles roles = new Roles();
roles.initRoles(roleNode);
if(roles.getSize() <=0)
{
throw new SystemException("SessionAction.execute()","ERR-00001","对不起,您不是本系统用户");
}
uc.setRoles(roles);
/*Vector attrTypeV = new Vector();
Vector attrItemV = new Vector();
nodes = doc.getElementsByTagName("attr");
if(nodes.getLength()>0){
nodes = nodes.item(0).getChildNodes();
for(int i=0;i<nodes.getLength();i++){
Node tnode = nodes.item(i);
String typeno = tnode.getAttributes().getNamedItem("typeno").getNodeValue();
String typename = tnode.getAttributes().getNamedItem("typename").getNodeValue();
attrTypeV.add(new String[]{typeno,typename});
//Vector iv = new Vector();
NodeList inodes = tnode.getChildNodes();
for(int j=0;j<inodes.getLength();j++){
Node inode = inodes.item(j);
String itemno = inode.getAttributes().getNamedItem("itemno").getNodeValue();
String itemname = inode.getAttributes().getNamedItem("itemname").getNodeValue();
int done = 0;
for(int k=0;k<attrItemV.size();k++){
String[] sss = (String[])attrItemV.get(k);
if((typeno+itemno).equals(sss[0])){
done = 1 ;
}
}
if(done == 0)
attrItemV.add(new String[]{typeno+itemno,itemname});
}
//ArrayList vvv = new ArrayList();
//vvv.add(typeno);
//vvv.add(iv);
//attrItemV.add(vvv);
}
}
uc.setAttrItems(attrItemV);
uc.setAttrTypes(attrTypeV);*/