[急切求助]页面加载到WebBrowse中无法提交表单。。。

aa_22_999 2010-11-02 08:21:31
下面的代码想实现:把移动宽带连接的Web网站加载到WebBrowse控件中,
自动输入用户名和密码,然后自动提交表单。

现在可以实现加载页面,并向用户名和密码文本框中填充数据,
但是提交时没反应,好象F5刷新页面一样,
也没跳转到提示用户名和密码错误的页面。

请大家帮忙看下原因,谢谢。。。

// WebTestDlg.h : header file
class CWebTestDlg : public CDialog
{
CWebBrowser2 m_ctrlWeb;
};

// WebTestDlg.cpp : implementation file
//引用HTML相关头文件
#include <atlbase.h>
CComModule _Module;
#include <mshtml.h>
#include <atlcom.h>

#include <string>
using namespace std;

//声明自动提交表单的函数
void PutFormValue(IHTMLDocument2 * pIHTMLDocument2);

BOOL CWebTestDlg::OnInitDialog()
{
//默认打开移动宽带网站
m_ctrlWeb.Navigate("http://221.178.143.198:7001/style/default/default.jsp",NULL,NULL,NULL,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}

//实现自动提交表单
void PutFormValue(IHTMLDocument2 * pIHTMLDocument2)
{
if(!pIHTMLDocument2)
return;

HRESULT hr;
CComBSTR bstrTitle;

//获取加载页面的标题
pIHTMLDocument2->get_title( &bstrTitle );
USES_CONVERSION;

CComQIPtr<IHTMLElementCollection>spElementCollection;
hr = pIHTMLDocument2->get_forms( &spElementCollection );
if (FAILED(hr))
{
AfxTrace(_T("获取表单的集合 IHTMLElementCollection 错误"));
return;
}

long nFormCount=0;

//获取表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
AfxTrace( _T("获取表单数目错误"));
return;
}

for(long i=0; i<nFormCount; i++)
{
IDispatch *pDisp = NULL; //取得第 i 项表单
hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
if ( FAILED( hr ) ) continue;

CComQIPtr< IHTMLFormElement > spFormElement = pDisp;
pDisp->Release();

long nElemCount=0; //取得表单中 域的数目
hr = spFormElement->get_length( &nElemCount );
if ( FAILED( hr ) ) continue;

CString strName;
CString strVal;
for(long j=0; j<nElemCount; j++)
{
CComDispatchDriver spInputElement; //取得第 j 项表单域

hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
if ( FAILED( hr ) ) continue;

CComVariant vName,vVal,vType; //取得表单域的 名,值,类型
hr = spInputElement.GetPropertyByName( L"name", &vName );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"value", &vVal );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"type", &vType );
if( FAILED( hr ) ) continue;

LPCTSTR lpName = vName.bstrVal?
OLE2CT( vName.bstrVal ) : _T("NULL"); //未知域名
LPCTSTR lpVal = vVal.bstrVal?
OLE2CT( vVal.bstrVal ) : _T("NULL"); //空值,未输入
LPCTSTR lpType = vType.bstrVal?
OLE2CT( vType.bstrVal ) : _T("NULL"); //未知类型

strName = lpName;
strName.TrimLeft();
strName.TrimRight();

//向用户名文本框内填充数据
if (strName == "UserName")
{
TCHAR szText[32] = "123456";
CComVariant vMyVal = (LPCTSTR)(szText);
spInputElement.PutPropertyByName( L"value",&vMyVal);
}

//向密码文本框内填充数据
if (strName == "PassWord")
{
TCHAR szText[32] = "789";
CComVariant vMyVal = (LPCTSTR)(szText);
spInputElement.PutPropertyByName( L"value",&vMyVal);
}
} //for(long j=0; j<nElemCount; j++)

//提交表单,感觉象是按了F5刷新
//页面没跳转到提示用户名和密码错误的页面
spFormElement->submit();
} //for(long i=0; i<nFormCount; i++)
}

//测试自动提交表单
void CWebTestDlg::OnButton1()
{
//创建IHTMLDocument2类型的对象
CComPtr <IDispatch> spDispDoc;
spDispDoc = m_ctrlWeb.GetDocument();
CComQIPtr<IHTMLDocument2> spDocument2 = spDispDoc;
if (!spDocument2)
return;

PutFormValue(spDocument2);
}
...全文
531 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
现在可以通过mouse_event模拟鼠标点击。
如果调整分辨率或换显示器可能就没有效果。

查找form中取所有的a标签,
找到“登 录”标签,触发onclick

这样是否可以实现呢?
向立天 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 aa_22_999 的回复:]
准备用下面的代码,模拟鼠标单击
如何获取那个Butto连接的 坐标呢???
//
::SetCursorPos(20,20);
Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(1000);
[/Quote]
截个图用绘图软件量一下
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
准备用下面的代码,模拟鼠标单击
如何获取那个Butto连接的 坐标呢???
//
::SetCursorPos(20,20);
Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(1000);

aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 xianglitian 的回复:]
不用
你把该填的都填好
mouse_event和你自己用鼠标点登录按钮的效果是一样的
[/Quote]

谢谢,我抓紧试下看看。。。
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
请教个问题:
"登 录"是登录按纽的什么属性值呢?
id值得,name值, value值都不是么?
是图片按纽,还是URL链接呢?

<tr>
<td height="50" colspan="2" align="center">
<div class="log">
<a href="#" onclick="onCheck()">登 录</a>
</div>
</td>
</tr>

通过枚举窗体DOM元素,没有获取到提交这个按纽;
向立天 2010-11-03
  • 打赏
  • 举报
回复
不用
你把该填的都填好
mouse_event和你自己用鼠标点登录按钮的效果是一样的
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 xianglitian 的回复:]
引用 14 楼 aa_22_999 的回复:
引用 11 楼 xianglitian 的回复:
实在不行就一切准备就绪后用mouse_event模拟一个鼠标点击进行提交

--------------------
如何模拟鼠标点击呢,是否需要获取句柄呢?

不用窗口句柄
计算好点击位置就可以了
你可以参考以下资料
http://baike.baidu.com/view/1080……
[/Quote]

//=====================
<td height="50" colspan="2" align="center">
<div class="log">
<a href="#" onclick="onCheck()">登 录</a>

是否需要获取登陆按纽的一些参数呢?
向立天 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 aa_22_999 的回复:]
引用 11 楼 xianglitian 的回复:
实在不行就一切准备就绪后用mouse_event模拟一个鼠标点击进行提交

--------------------
如何模拟鼠标点击呢,是否需要获取句柄呢?
[/Quote]
不用窗口句柄
计算好点击位置就可以了
你可以参考以下资料
http://baike.baidu.com/view/1080208.htm
sos_2010123 2010-11-03
  • 打赏
  • 举报
回复
sos_2010123 2010-11-03
  • 打赏
  • 举报
回复
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
<td height="50" colspan="2" align="center">
<div class="log">
<a href="#" onclick="onCheck()">登 录</a>

没有name,id和value, "登 录"不知道是什么属性?
如何模拟鼠标单击事件呢?
请大家帮忙看下,谢谢...
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
//移动宽带连接的网站
http://221.178.143.198:7001/style/default/default.jsp

提交页面的HTML代码:
<body>
<form id="form1" name="form1" method="post" action=""style="margin: 0; padding: 0;">
<td height="23" align="right" valign="bottom" nowrap>账  号</td>
<input type="hidden" name="usertype" id="usertype" value="">
<input type="text" name="UserName" id="UserName" class="input"vmaxlength=40 value="" />

<td height="23" align="right" valign="bottom" nowrap>密  码</td>
<input type="password" class="input" name="PassWord" id="PassWord" value="">

<input type="hidden" name="isCookie" id="isCookie" value = "false">
<input type="hidden" name="province" id="province" value="">
<input type="hidden" name="cookieType" id="cookieType" value = "-1">

<input type="radio" name="cookie" id="cookie" value="1" onclick="remeberPassword(1)" />记住帐号</font>
<input type="radio" name="cookie" id="cookie" value="2" onclick="remeberPassword(2)" />记住密码</font>
<input type="radio" name="cookie" id="cookie" value="0" onclick="remeberPassword(0)" />取消</font>

<td height="50" colspan="2" align="center">
<div class="log">
<a href="#" onclick="onCheck()">登 录</a>

<td id="sysMessage" style="color: #4651a0;">尊敬的宽带客户:
如您的宽带账户即将到期,可至各移动营业厅办理续费,包年宽带续费仅需600元,赠送200元礼品(食用油、超市券。。。。。。任您选择)。详询营业厅和10086。</td>
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
//移动宽带连接的网站
http://221.178.143.198:7001/style/default/default.jsp

自己用鼠标在WebBrowse控件中点击提交后,用SNIFFER抓的数据包;
(1)192.168.1.2--->221.178.143.198
POST /browser_auth_init.jsp?isCookie=true HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*
Referer: http://221.178.143.198:7001/style/default/default.jsp
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)
Host: 221.178.143.198:7001
Content-Length: 94
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=pc7VMQTLB5dkQxRkSRzZ2XBF6bQvr9DqwTbSx9V4jTB1BM6x261Q!-849807394

usertype=&UserName=111111111&PassWord=2222222222&isCookie=true&province=&cookieType=1&cookie=1
(2)192.168.1.2<---221.178.143.198
HTTP/1.1 200 OK
Date: Tue, 02 Nov 2010 22:18:20 GMT
Content-Length: 555
Content-Type: text/html; charset=gb2312






<html>
<head></head>
<body>
<form name="form1" method="post" action="/browser_auth.jsp">
<input type="hidden" name="UserName" value="111111111">
<input type="hidden" name="PassWord" value="2222222222">
<input type="hidden" name="verifycode" value="null">
<input type="hidden" name="isCookie" value="true">
<input type="hidden" name="cookieType" value="1">
<input type="hidden" name="errorCode" value="">
</form>

<script language="javascript">
//alert("111111111");
document.form1.submit();
</script>
</body>
</html>
(3)192.168.1.2--->221.178.143.198
POST /browser_auth.jsp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*
Referer: http://221.178.143.198:7001/browser_auth_init.jsp?isCookie=true
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)
Host: 221.178.143.198:7001
Content-Length: 92
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=pc7VMQTLB5dkQxRkSRzZ2XBF6bQvr9DqwTbSx9V4jTB1BM6x261Q!-849807394

UserName=111111111&PassWord=2222222222&verifycode=null&isCookie=true&cookieType=1&errorCode=
(4)192.168.1.2<---221.178.143.198
HTTP/1.1 200 OK
Date: Tue, 02 Nov 2010 22:18:20 GMT
Content-Length: 355
Content-Type: text/html; charset=gb2312







<html>
<body>
<form name="form1" method="post" action="/browser_auth_ok_init.jsp">
<input type="hidden" name="UserName" value="111111111">
<input type="hidden" name="errorCode" value="-1">
</form>

<script>
document.forms[0].action = "/browser_auth_fail.jsp";
document.forms[0].submit();
</script>

</body>
</html>
(5)192.168.1.2--->221.178.143.198
POST /browser_auth_fail.jsp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*
Referer: http://221.178.143.198:7001/browser_auth.jsp
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)
Host: 221.178.143.198:7001
Content-Length: 31
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=pc7VMQTLB5dkQxRkSRzZ2XBF6bQvr9DqwTbSx9V4jTB1BM6x261Q!-849807394

UserName=111111111&errorCode=-1
(6)192.168.1.2<---221.178.143.198
HTTP/1.1 200 OK
Date: Tue, 02 Nov 2010 22:18:20 GMT
Content-Length: 1685
Content-Type: text/html; charset=gb2312





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
<!--
.t1 {font-size:9pt;color:#464646}
.s {font-size:10pt;color:#000000}
-->
</style>
</head>

<body><br />
<table width="300" height="52" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><img src="./style/default/images/litban.gif" width="300" height="52" /></td>
</tr>
</table>
<table width="300" height="35" border="0" align="center" cellpadding="0" cellspacing="0" class="error">
<tr>
<td align="center">登录出错页面</td>
</tr>
</table>
<table width="260" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="3" background="./style/default/images/litline.gif"></td>
</tr>
</table>
<table width="300" height="105" border="0" align="center" cellpadding="0" cellspacing="0" class="info">
<tr>
<td width="90" align="center" valign="middle"><img src="./style/default/images/litwarn.gif" alt="错误" width="69" height="65" /></td>
<td width="210" align="left" valign="middle" class
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
AfxMessageBox("准备提交");
spFormElement->submit();
AfxMessageBox("提交结束");


提交之前:


提交之后,感觉象是按了F5刷新页面
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xianglitian 的回复:]
实在不行就一切准备就绪后用mouse_event模拟一个鼠标点击进行提交
[/Quote]
--------------------
如何模拟鼠标点击呢,是否需要获取句柄呢?
aa_22_999 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 lsq19871207 的回复:]
你把 CComQIPtr< IHTMLFormElement > spFormElement 设置成全局的试试
[/Quote]
---------------
修改成全局的还是不可以...
信阳毛尖 2010-11-02
  • 打赏
  • 举报
回复
你把 CComQIPtr< IHTMLFormElement > spFormElement 设置成全局的试试
向立天 2010-11-02
  • 打赏
  • 举报
回复
实在不行就一切准备就绪后用mouse_event模拟一个鼠标点击进行提交
aa_22_999 2010-11-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dog10521350 的回复:]
你设置断点单步调式一下,可以调试到 for(long i=0; i<nFormCount; i++)
里面去吗?
[/Quote]
------------
单步可以调试进去,并且可以给文本框赋值,
就是没办法进行提交submit...
加载更多回复(9)

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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