怎么样取得网页上的验证码自动登陆
大家好,小弟现在想弄一个网页登陆程序,登陆的时候网页上有验证码就是一个小图片,请问用BCB怎么取得图片上的验证码,自动输入,或者把图片单独显示出来让用户输入呢,我用TCppWebBrowser控制,主要不想在TCppWebBrowser里直接输入,想自己写个输入用户名密码的地方,谢谢
[解决办法]
那种随即码就是为了防止机器注册的。如果用传统的方法去下载图片得到的数字肯定和你看到的页面是不一样的。所以你可以考虑实现把当前的图片复制下来的方法。写了段小程序没写注释应该很容易看懂。把图片复制下来后是显示在控件上,还是怎么处理就看你自己喜欢了。 代码里我把图片存了下来。
由于我的测试网络内容比较简单,代码里就没有具体去定位图片。楼主要用还需要先分析具体的页面。然后去定位到需要的图片接口上。
以下是代码:
//---------------------------------------/*测试用html 代码<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE>test</TITLE> </HEAD> <BODY> <center> <div> 测试网页 </div> <div> <img src="yy1.jpg" border="0"> </div> </center> </BODY></HTML>*/#include <vcl.h>#include "objbase.h"#include <iostream>#include <mshtml.h>#pragma hdrstop//---------------------------------------#pragma argsusedusing namespace std;HRESULT (WINAPI *ObjectFromLresult)(LRESULT lResult,REFIID riid,WPARAM wParam,void** ppvObject);HANDLE IEHandle;bool LoadApi() { CoInitialize(NULL); HINSTANCE hInst=::LoadLibrary("OLEACC.DLL"); if(hInst==NULL) return false; ObjectFromLresult=(HRESULT (WINAPI *)(LRESULT lResult,REFIID riid,WPARAM wParam,void** ppvObject))GetProcAddress(hInst,"ObjectFromLresult"); if(ObjectFromLresult==NULL) { FreeLibrary(hInst); return false; } return true;}bool GetIHTMLDocument(HANDLE m_hwnd,void **pvoid) { HRESULT Result; UINT nMsg=::RegisterWindowMessage(("WM_HTML_GETOBJECT")); LRESULT lRes; ::SendMessageTimeout(m_hwnd,nMsg,0L,0L,SMTO_ABORTIFHUNG,1000,(DWORD*)&lRes); Result=ObjectFromLresult(lRes, __uuidof(IHTMLDocument2),0,pvoid); if(Result==S_OK) return true; return false;}int main(int argc, char* argv[]) { IHTMLDocument2 *pDoc; IHTMLElementCollection *pImages; VARIANT imgIndex; IDispatch *pimgDispatch; IHTMLElement *pBody; IHTMLTextContainer *pTextCont=NULL; IDispatch *pdispCtrlRange = NULL; IHTMLControlRange *pCtrlRange = NULL; IHTMLControlElement *pCtrlElement = NULL; BSTR bstrCommand = SysAllocString(L"Copy"); HRESULT hResult; VARIANT v1; VariantInit(&v1); VARIANT_BOOL v2; IEHandle = (HANDLE)132040; if(!LoadApi()) { cout<<"loadapi error"<<endl; return 0; } if(GetIHTMLDocument(IEHandle, (void **)&pDoc) == false) { cout<<"get document2 interface error"<<endl; return 0; } if(pDoc->get_images(&pImages) != S_OK) { cout<<"get images error"<<endl; return 0; } memset(&imgIndex, 0x00, sizeof(VARIANT)); imgIndex.vt = VT_I4; imgIndex.iVal = 0; if(pImages->item(imgIndex, imgIndex, &pimgDispatch) != S_OK) { cout<<"get image item error"<<endl; return 0; } pDoc->get_body(&pBody); pBody->QueryInterface(IID_IHTMLTextContainer, (void**)&pTextCont); if(!pTextCont) { cout<<"get textcont error"<<endl; return 0; } pTextCont->createControlRange(&pdispCtrlRange); if(!pdispCtrlRange) { cout<<"create control range error"<<endl; return 0; } pdispCtrlRange->QueryInterface(IID_IHTMLControlRange, (void**)&pCtrlRange); if(!pCtrlRange) { cout<<"get CtrlRang interface error"<<endl; return 0; } pimgDispatch->QueryInterface(IID_IHTMLControlElement, (void**)&pCtrlElement); if(!pCtrlElement) { cout<<"get CtrlElement interface error"<<endl; return 0; } hResult = pCtrlRange->add(pCtrlElement); if(SUCCEEDED(hResult)) { hResult = pCtrlRange->execCommand(bstrCommand, VARIANT_FALSE, v1, &v2); } pCtrlElement->Release(); pCtrlRange->Release(); pdispCtrlRange->Release(); pTextCont->Release(); pimgDispatch->Release(); pBody->Release(); pImages->Release(); pDoc->Release(); SysFreeString(bstrCommand); OpenClipboard(NULL); HANDLE hBitmap = GetClipboardData(CF_BITMAP); //get the bmp handle! Graphics::TBitmap *pBitmap = new Graphics::TBitmap(); pBitmap->LoadFromClipboardFormat(CF_BITMAP, (int)hBitmap, 0); pBitmap->SaveToFile("C:\\test_webimg.bmp"); delete pBitmap; CloseClipboard(); cout<<"ok"<<endl; system("pause"); return 0;}