★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);*/



...全文
233 3 打赏 收藏 转发到动态 举报
写回复
用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控件。
Delphi XE2 正式版的破解程序,有效哦。 Delphi XE2 官方完整 delphicbuilder_xe2_win_dl.iso下载地址: http://altd.embarcadero.com/download/radstudio/xe2/delphicbuilder_xe2_win_dl.iso 文件大小: 2.33G MD5: de8b9f897e2ad678643b64898470b707 Delphi Prism XE2 - ISO 1.28G http://altd.embarcadero.com/download/prism/xe2/prism_xe2_win.iso RadPHP XE2 - ISO 343M http://altd.embarcadero.com/download/radphp/xe2/radphp_xe2_win.iso 建议大家用迅雷下载 ************************************** 英巴卡迪诺发布新版RAD Studio XE2开发工具套件 日前,英巴卡迪诺宣布发布新版RAD Studio XE2软件开发工具套件,新版包含了10多年来最深入全面的特性升级。RAD Studio XE2包含了新版本的Delphi、C++Builder、Prism和RadPHP。主要特性包括完整的Delphi 64位Windows支持,通过全新的FireMonkey富商业应用平台支持Mac OSX和iOS,以及通过RadPHP XE2支持安卓web和移动应用开发。 对Delphi XE2和C++Builder XE2来说,一个极其重要的新特性就是FireMonkey,业界第一款用于创建商业软件的原生的能全面利用到CPU和GPU计算能力的富应用平台。使用 FireMonkey,Delphi和C++Builder开发人员能开发出视觉绚丽的高清和3D应用程序,并与后台的企业级数据库如Oracle、微软 SQL Server、IBM DB2、Sybase和其他数据库互联互通。FireMonkey应用以原生方式运行在Windows PC和Mac上,可以完全利用到现代GPU的处理能力,以不可置信的细节处理能力和优异的性能表现展现数据和用户界面。FireMonkey还可以使 Delphi XE2用户为移动(iOS)设备创建原生的高清和3D应用程序。 Delphi和C++Builder为应用开发提供原生开发方式,而RadPHP XE2则支持web开发和移动设备应用的开发。使用RadPHP XE2,开发人员可以轻松为移动智能电话和平板电脑创建支持触碰和UI优化的web应用。开发人员可以部署他们的移动Web应用为原生移动应用到苹果公司的iOS应用商店和安卓的应用市场。在他们的移动Web应用,可以直接访问移动设备的硬件,如相机、GPS和重力感应器。 其他新增特性包括: Delphi XE2 和C++Builder XE2 Delphi XE2包括完整的Windows 64位对FireMonkey、编译器、调试器、RTL 和VCL的支持; dbExpress具有新的对InterBase XE, FireBird 2.5, SQL Anywhere 12 和ODBC的连接能力; 新的DataSnap移动连接器,带有原生的平台组件和示例以创建从移动客户端到Delphi DataSnap服务器的连接,支持安卓(Java), iOS (Objective C), 黑莓(Java) 和Windows Phone 7 (C#); 新增和更新的扩展工具,包括FastReport,用于快速设计和生成报表;Documentation Insight,用于Delphi的源代码文档工具; TeeChart、IBX、InstallAware、IP*Works、Aqtime、FinalBuilder、IntraWeb和Indy 的64位版本。 TeeChart、Indy和FinalBuilder还支持Mac平台; C++Builder XE2还包含了新的代码审计和QA度量工具; LiveBindings使用户能连接任意类型的信息到任意的FireMonkey用户界面和图形对象上。用户可以绑定实时数据到标准的用户界面控件、高清或3D图形元素上,以创建可视化任意类型数据的崭新方式; RadPHP: 集成式为移动优化的Web开发; 部署应用到Web, iOS, 或安卓; 扩展的Zend框架组件集; Embarcadero Prism: 来自RemObjects的全新的.NET Oxygene编译器; 匿名接口实现; FastReport.net

1,316

社区成员

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

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