怎样用VC代码实现对一个网页填表并按确定按钮提交?

zippo 2006-03-13 09:59:32
类似登录页面,现可以读出网页,但不知道怎样找到用户名及密码编辑框,找到之后该怎么填写字符串进去?还有就是怎样按下按钮提交,谢谢!
...全文
269 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kudeet 2006-03-14
  • 打赏
  • 举报
回复
How To Automate Internet Explorer to POST Form Data
http://support.microsoft.com/kb/q167658/

///////////////////////////////////////////////////////////////////////////
//SDK post
///////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "winsock.h"
#pragma comment(lib,"ws2_32.lib")
#define winsock_version 0x0101
void main()
{
//I create C:\Inetpub\wwwroot\test\test.asp ,start the web service
//start my program, the result is OK.
//If it works,it is written by masterz,otherwise I don't know who write it.
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet;
const char* host_name="127.0.0.1";
char* req="POST /test/test.asp HTTP/1.0\r\n"
"From: local\r\n"
"User-Agent: post_test/1.0\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 20\r\n\r\n"
"type=12345&name=aaaa";
if(WSAStartup(winsock_version,&wsadata))
printf("can't initial socket");
lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent is null");
hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
saServer.sin_family = AF_INET;
// Use def. now, need to handle general case
saServer.sin_port = htons(80);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
printf("can't connect");
closesocket(hsocket);
return;
}
else
printf("connected with %s\n",host_name);
nRet = send(hsocket, req, strlen(req), 0);
if (nRet == SOCKET_ERROR)
{
printf("send() failed");
closesocket(hsocket);

}
else
printf("send() OK\n");
char dest[1000];
nRet=1;
while(nRet>0)
{
nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);
if(nRet>0)
dest[nRet]=0;
else
dest[0]=0;
printf("\nReceived bytes:%d\n",nRet);
printf("Result:\n%s",dest);
}
}
Kudeet 2006-03-14
  • 打赏
  • 举报
回复
///////////////////////////////////////////////////////////////////////////////////////
//click submit button of IE window
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////////////////////////////////////
void CGetIESrcDlg::NavigateToUrl()
{
// Import the following files in your stdafx.h
// #import <mshtml.tlb> // Internet Explorer 5
// #import <shdocvw.dll>
// Refer to "Connect to Internet Explorer Instances, From your own Process. " in www.codeguru.com
SHDocVw::IShellWindowsPtr m_spSHWinds;
CoInitialize(NULL);
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
MSHTML::IHTMLElementPtr spHtmlElement;
if(spHtmlDocument==NULL)
continue;
spHtmlDocument->get_body(&spHtmlElement);
if(spHtmlDocument==NULL)
continue;
HRESULT hr;
MSHTML::IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
MSHTML::IHTMLElement* pElem=NULL;
_variant_t index;
index.vt=VT_I4;
index.intVal=0;
_variant_t name("Submit");
IDispatchPtr disp;
disp=pColl->item(name,index);
if(disp==NULL)
hr=E_FAIL;
else
{
hr=disp->QueryInterface(&pElem);
}
if (SUCCEEDED(hr)&& pElem != NULL)
{
//
BSTR bstrhtml;
pElem->get_outerHTML(&bstrhtml);
CString str(bstrhtml);
AfxMessageBox(str);
pElem->click();
pElem->Release();
}
pColl->Release();
}
}

}
}

}
else {
AfxMessageBox("Shell Windows interface is not avilable");
}
CoUninitialize();
}

yinzhaohui 2006-03-14
  • 打赏
  • 举报
回复
这需要进行Web分析和了解HTML协议
boluoCTO 2006-03-14
  • 打赏
  • 举报
回复
用Chtmlview可以读出页面,你可以把button做到页面写教本来操作,多方便阿。
如果你实在要用VC的BUTTON,那也是有办法的,为Chtmlview的GetContainer( )函数可以提供和document.all()差不多的功能。只是用起来很复杂,可以找找相关文档。希望对你有帮助

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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