如何用 CppWebBrowser 自动测试网站

faretel 2008-08-27 12:46:24
请问高手如何用 CppWebBrowser 写程序进行自动测试网站呢?
我试过这种方法: 先用WebBrowser读取指定的网页内容,然后通过IHTMLDocument2接口分析HTML内容.
然后让程序填表单数据, 提交
pFormElement->submit(); //提交这一步程序没有报错

但如何在提交成功后获得新的一页的HTML内容, 然后让程序再填新的数据, 再次提交...... 直到完成最后一页任务呢?
在第一页数据成功提交后, 网站应该返回第二页内容, 但我试用如下方法取新内容, 不行, Memo1 中显示的还是原来第一页的内容:
CppWebBrowser1->Update();
pDisp = CppWebBrowser1->Document;
pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc2);
pDoc2->get_body(&body);
body->get_outerHTML(&source) ;
Memo1->Text=AnsiString(source);

...全文
192 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunyuchuan86 2008-09-08
  • 打赏
  • 举报
回复
帮忙顶了.蹭点儿辛苦分儿.
柯本 2008-09-08
  • 打赏
  • 举报
回复
会不会是Cookie的问题,我用TIdHTTP做过网站提交,只是测试,供你参考

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TStringList *s=new TStringList();
TStream *m=new TMemoryStream();
int i;

s->Add("username=yourname&password=12345678&domain=test.com&do.login=%B5%C7%C2%BC");
// http->Request->Accept="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
http->Request->ContentType ="application/x-www-form-urlencoded";
// http->Request->Referer="http://mail.test.com/cgi-bin/wmail?index=1";
// http->Request->Connection="keep-alive";
// http->Request->ContentLanguage="zh-cn";
// http->Request->ContentEncoding="gzip, deflate";
// http->Request->UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Maxthon; .NET CLR 1.1.4322)";
// http->Request->From="http://mail.test.com/cgi-bin/wmail?index=1";
http->Host="http://mail.test.com";

// http->Request->ContentLength=67;
try
{
http->Post("http://mail.test.com/cgi-bin/wmail",s,m);
// http->Post("http://www.test.com/p.php",s,m);
}
catch(...)
{
String Cookie,CookieList;
Memo1->Lines->Add("---------------------------");
Memo1->Lines=(http->Response->ExtraHeaders);
Memo1->Lines->Add("---------------------------");
// Memo1->Lines=(http->Request->ExtraHeaders);

for(i=0;i<http->Response->ExtraHeaders->Count;i++)
{
if (UpperCase(http->Response->ExtraHeaders->Strings[i].SubString(1,10)) =="SET-COOKIE")
{
Cookie =Trim(http->Response->ExtraHeaders->Strings[i].SubString(12,1000));
Cookie =Cookie.SubString(1,Cookie.Pos(";"));
CookieList+=Cookie;
Memo1->Lines->Add(Cookie);
}
}
http->Request->ExtraHeaders->Add("Cookie: "+CookieList);
http->Request->ContentType="text/plain";
m->Seek(0,soFromBeginning);
// http->Request->Referer="http://mail.test.com/cgi-bin/wmail/wn/?folder=INBOX&form=main";
http->Get("http://mail.test.com/cgi-bin/wmail/wn/?folder=INBOX&form=main",m);
}
//http->Post("http://www.test.com/p.php",s,m);
m->Seek(0,soFromBeginning);
Memo2->Lines->LoadFromStream(m);

delete m;
delete s;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TStringList *s=new TStringList();
TStream *m=new TMemoryStream();
int i,ct;
String x,t,k;
unsigned char c;

s->Add("username=yourname&password=12345678&domain=test.com&do.login=%B5%C7%C2%BC");
http->Request->Accept="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*";
http->Request->ContentType ="application/x-www-form-urlencoded";
http->Request->Referer="http://mail.test.com/cgi-bin/wmail?index=1";
http->Request->Connection="keep-alive";
http->Request->ContentLanguage="zh-cn";
http->Request->ContentEncoding="gzip, deflate";
http->Host="mail.test.com";

http->Request->ContentLength=67;
try
{
http->Post("http://mail.test.com/cgi-bin/wmail?index=1",s,m);
}
catch(...)
{
m->Seek(0,soFromBeginning);
Memo2->Lines->LoadFromStream(m);
m->Seek(0,soFromBeginning);

ct=m->Size;
for(i=0;i<ct;i++)
{
if(i%10==0)
{
Memo1->Lines->Add(x+" "+k);
x="" ;
k="";
}
m->Read(&c,1);
t.printf("%02x ",c);
k+=(char )c;
x+=t;
}
}
delete m;
delete s;
}
//-----------------------------
faretel 2008-08-27
  • 打赏
  • 举报
回复
没人试过吗?
有 NMhttp, Indy 的例子也行, 但不要JAVA的.

1,316

社区成员

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

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