★JAVA用户登后代码在C++ Builder中如何实现?谢谢!

wwei07 2012-11-04 10:10:55
下面JAVA代码是用户登后,得到返回的报文,判断登陆是否成功。

其中:public Communicate(String msg) 在c++ Builder中如何实现?


报文格式:
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);*/



...全文
246 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mwq831920 2013-03-01
  • 打赏
  • 举报
回复
同意一楼的看法用IdHTTP,以前用它做过一个web网站的登录,贴其中一个方法代码给你参考下: // 以Web形式登录 bool __fastcall TMainForm::Login(TIdHTTP *aIdHTTP, AnsiString aUserNameStr, AnsiString aPasswordStr, AnsiString aVerifyStr, TEdit *tedit) { TStringStream *PostData, *RespData; AnsiString tmpStr; AnsiString aWebStr; /* String str=Format(Const_PostLoginInfo, OPENARRAY(TVarRec,(aUserNameStr, Preprocess(aPasswordStr, aVerifyStr), aVerifyStr))); PostData= new TStringStream( Format(Const_PostLoginInfo, OPENARRAY(TVarRec,(aUserNameStr, Preprocess(aPasswordStr, aVerifyStr), aVerifyStr))) + Const_PostLoginStr); */ PostData = new TStringStream (Format(Const_PostLoginInfo, OPENARRAY(TVarRec, (aUserNameStr, aPasswordStr, aVerifyStr))) + Const_PostLoginStr); //RespData = new TStringStream; bool Result = false; aWebStr = ""; try { aIdHTTP->ReadTimeout = 15000; aIdHTTP->Request->Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, */*"; aIdHTTP->Request->ContentType = "application/x-www-form-urlencoded"; aIdHTTP->Request->Host = "xxx.com"; aIdHTTP->Request->Referer = "http://xxx.com/cgi-bin/login?appid=15000101"; aIdHTTP->Request->UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"; aIdHTTP->Request->AcceptEncoding = "gzip, deflate"; aIdHTTP->Request->URL = "http://xxx.com/login"; // aIdHTTP.Request.SetHeaders; // aIdHTTP->Request->RawHeaders->Values["Cookie"] = "Cookie:"+Cookie; TEncoding *encoding; RespData = new TStringStream("",encoding->GetEncoding(65001),true);//936 is gb2312 aIdHTTP->Post("http://xxx.com/login", PostData, RespData); // tmpStr= Utf8ToAnsi(RespData->DataString); //WCHAR *wchar = new WCHAR[RespData->Size]; //myUTF8_UNICODE((char *)RespData->DataString.t_str(), RespData->Size, wchar); tmpStr=RespData->DataString; //tmpStr = wchar; // MultiByteToWideChar Memo1->Text = tmpStr; // if (strstr((char *)tmpStr.c_str(), "如果未能自动跳转,请<a href=\"http://xxx.com/index.php?mod=login\" target=\"_top\">点击</a>完成跳转") != NULL) { Result = true; tmpStr = SubString(aIdHTTP->Response->RawHeaders->Text, "Set-Cookie: pt2gguin=", ";"); Cookie_pt2gguin = tmpStr; tmpStr = SubString(aIdHTTP->Response->RawHeaders->Text, "Set-Cookie: uin=", ";"); Cookie_uin = tmpStr; tmpStr = SubString(aIdHTTP->Response->RawHeaders->Text, "Set-Cookie: skey=", ";"); Cookie_skey = tmpStr; tmpStr = SubString(aIdHTTP->Response->RawHeaders->Text, "Set-Cookie: ptcz=", ";"); Cookie_ptcz = tmpStr; Cookie = Cookie + " pt2gguin=" + Cookie_pt2gguin + "; uin=" + Cookie_uin + "; // 还有一个Cookie---- Login_Time需要保存 try { aIdHTTP->Get("http://xxx.com/" + ComboBox1->Text, RespData); } catch(Exception & e) { } tmpStr = SubString(aIdHTTP->Response->RawHeaders->Text, "Set-Cookie: login_time=", ";"); // MainForm->Memo1->Text=aIdHTTP->Response->RawHeaders->Text;return true; Cookie_login_time = tmpStr; Cookie += " login_time=" + Cookie_login_time + ";"; Randomize; Cookie_pvid = IntToStr(Random(10)) + IntToStr(Random(10)) + IntToStr (Random(10)) + IntToStr(Random(10)) + IntToStr(Random(10)) + IntToStr(Random(10)) + IntToStr(Random(10)) + IntToStr(Random(10)) + IntToStr (Random(10)) + IntToStr(Random(10)); Cookie += " pvid=" + Cookie_pvid + "; randomSeed=4372292;"; } else if (strstr(tmpStr.c_str(), "超时") > 0) aWebStr = "连接超时或者页面出错!"; else if (strstr(tmpStr.c_str(), "验证码") > 0) aWebStr = "验证码错误!"; else if (strstr(tmpStr.c_str(), "密码") > 0) aWebStr = "密码错误!"; else if (strstr(tmpStr.c_str(), "系统繁忙") > 0) aWebStr = "系统繁忙!"; tedit->Text = aWebStr; } catch(Exception & e) { Result = false; aWebStr = "连接超时或者页面出错!"; tedit->Text = aWebStr; } PostData->Free(); RespData->Free(); return Result; }
wwei07 2012-11-04
  • 打赏
  • 举报
回复
能否具体些,谢谢!
laowang2 2012-11-04
  • 打赏
  • 举报
回复
用idhttp控件。

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧