C++Builder 6 如何在网页中填写用户名和密码

xm200 2011-09-10 02:28:37
如题 求教在BCB6环境下,编写一个程序,能够打开指定的网页,并填写其中的用户名和密码等项目,然后自动提交登陆。请问如何实现,需要用哪些控件或者函数。谢谢!
...全文
711 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanguorewrite 2012-05-05
  • 打赏
  • 举报
回复
thank you so much. I have same question to ask.
neoliang007 2012-02-28
  • 打赏
  • 举报
回复
纠结中。。。。。==
cptang 2012-02-06
  • 打赏
  • 举报
回复
用第三方控件更简单
cleversuit
BCBPLC 2011-09-25
  • 打赏
  • 举报
回复
下次我试试看,如果成功,楼上就立功了,因为比老妖的简单多了。
BCBPLC 2011-09-25
  • 打赏
  • 举报
回复
楼上能不能用呢,关注,如果能这样用,肯定方便多了。
Bosman 2011-09-14
  • 打赏
  • 举报
回复
基本简单用法:

Variant v,v2;
v=WebBrowser->Document;
v=v.OlePropertyGet("All");
v2=v.OlePropertyGet("loginForm");//登录表单名
v=v2.OlePropertyGet("id");
v.OlePropertySet("value",登录名[必须是WideString类型]);
v=v2.OlePropertyGet("password");
v.OlePropertySet("value",密码[必须是WideString类型];
v2.OleProcedure("Submit");//表单提交
BCBPLC 2011-09-13
  • 打赏
  • 举报
回复
对象ie 就是关键对象
BCBPLC 2011-09-13
  • 打赏
  • 举报
回复
有了我这个封装后对象,操纵一个网页简单多了。
下面取网页学生分数的源程序:

#include <vcl.h>
#pragma hdrstop

#include "ActiveX.hpp"
#include "mainUnit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
#include "IE.h"
String curdir;
WideString htm="www\\chaFS.htm";
IE *ie=NULL;
String ff="Fs.xls";

bool finished=false;
void __fastcall TForm1::CppWebBrowser1DocumentComplete(TObject *Sender,
LPDISPATCH pDisp, Variant *URL)
{
finished=true;
}
//---------------------------------------------------------------------------
String Fs="";
String GetSubstr(String sub,int n)
{
int l=Fs.Length();
String ss="";
int w=Fs.Pos(sub);
if (w>0)
{
w+=sub.Length();
for (int i=0;i<n && w+i<l;i++)
{
Byte c=Fs[w+i];
if (c>=(Byte)' ')
ss=ss+(char)c;
else
break;
}
Fs=Fs.SubString(w,l-w);
}
return(ss);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
String tab=char(9);
String lr="\r\n";

int xh1=Xh1->Text.ToInt();
int xh2=Xh2->Text.ToInt();

ie->GoURL(htm);

String bt=lr+tab+tab+"考试证号"+tab+"姓名"+tab+"语文"+
tab+"数学"+tab+"综合"+tab+"英语"+tab+"总分"+lr+lr;

TFileStream *fs=new TFileStream(ff,fmCreate);
fs->Write((void *)bt.c_str(),bt.Length());

wchar_t *ws=new wchar_t[10240];

for (int i=xh1;i<=xh2;i++)
{
String zh=i;
while (!ie->Test())
Application->ProcessMessages();

while (!ie->SetText("number",zh))
Application->ProcessMessages();

finished=false;
while (!ie->BtnClick("denglu"))
Application->ProcessMessages();
while (!finished) // 等待下载完毕
{
Application->ProcessMessages();
if (Application->Terminated)
break;
}
ie->GetTextHTML(&ws);
Fs=WideString(ws);
String xm=GetSubstr("姓名: ",6); // 测试证号:zh
String yw=GetSubstr(" 语文",3).Trim();
String sx=GetSubstr("数学",3).Trim();
String zhe=GetSubstr("综合",3).Trim();
String zf=GetSubstr("总分",3).Trim();
String yy=GetSubstr("英语",3).Trim();

String lx=tab+tab+zh+tab+xm+tab+yw+
tab+sx+tab+zhe+tab+yy+tab+zf+lr;

fs->Write((void *)lx.c_str(),lx.Length());

if (Application->Terminated)
break;
finished=false;
ie->GoBack();
Caption=i;
}
delete []ws;
delete fs;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
static bool visible=true;
visible=!visible;
if (visible)
CppWebBrowser1->Show();
else
CppWebBrowser1->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
curdir=ExtractFilePath(Application->ExeName);
htm=curdir+htm;
ff=curdir+ff;

ie=new IE();
ie->Browser=CppWebBrowser1;
ie->GoURL(htm);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
ie->GoBack();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
ie->GoURL(htm);
}
//---------------------------------------------------------------------------


BCBPLC 2011-09-13
  • 打赏
  • 举报
回复
老妖对这个老问题已不屑一顾,我看了老妖的文章后,曾把此功能封装了一下,
调用简单多了,曾把一个网站的所有学生分数用BCB程序一页一页的取了过来。
封装的头片段 :
#include "mshtml.h"
typedef
IHTMLDocument2* p_IHTMLDocument2;
typedef
class IE
{
public:
TCppWebBrowser *Browser;
IHTMLDocument2 *pHTMLDoc; // 当前用的Object
IHTMLDocument2 *pHTMLDoc0; // 第一个Document
p_IHTMLDocument2 *pHTMLDoc_i; // 所有Frames的Object[i]
int pHTMLDoc_n; // Frames的数目
IE();
~IE();
bool Test();
void GetCookie(wchar_t **cookie);
void SetCookie(wchar_t *cookie);
int GetFrame();
void SetFrame(int i);
bool FindForm(String s);
bool BtnClick(String btn);
bool GetInnerHTML(wchar_t **ws);
bool GetOuterHTML(wchar_t **ws);
bool GetTextHTML(wchar_t **ws);
bool Submit(String form);
bool SetFormTarget(String fm,String target);
bool SetTarget(String target);
void SetAllTarget(String target);
bool SetText(String fd,String val);
bool GetText(String fd,wchar_t **ws);
bool SetUploadFile(String fd,String val);
bool GetUploadFile(String fd,wchar_t **ws);
bool SetTextArea(String fd,String val);
void GoURL(String url);
String GetURL();
void GoBack();
void Rele();
} IE;
ccrun.com 2011-09-13
  • 打赏
  • 举报
回复
这个操作简单,用mshtml提供的IHTMLDocument2接口完全可以搞定。
代码懒的写了,自己搜一下:

google "site:csdn.net IHTMLDocument2 ccrun 表单"
yang_jnu 2011-09-13
  • 打赏
  • 举报
回复
这个我以前我也试过,用的是MFC控件,还要用COM接口.....
xtayaitak 2011-09-12
  • 打赏
  • 举报
回复
不懂太顶。。
BCBPLC 2011-09-11
  • 打赏
  • 举报
回复
找老妖。

1,317

社区成员

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

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