首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

用C++builder兑现网页登录并保存网页中表格数据

2012-10-31 
用C++builder实现网页登录并保存网页中表格数据如题,如何不通过网页方式登录,直接用c++builder编程方式实

用C++builder实现网页登录并保存网页中表格数据
如题,如何不通过网页方式登录,直接用c++builder编程方式实现登录,在登录成功后,获取主页中的某一个表格中的数据并保存到server2000中?

请给个例子,如果分不够可以再加,谢谢了

[解决办法]
wininet 库里已经封装好了。登陆估计用的是post 如果是get那就更简单了。

这里有个ms 的post的例子

#include <Windows.h>
#include <WinINet.h>
#include <stdio.h>

BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize);
#define BUFFSIZE 500

void main( int argc, char **argv )
{
DWORD dwPostSize;

if (argc < 4)
{
printf( "Usage: Bigpost <Size> <Server> <Path> \n ");
printf( " <Size> is the number of KB to POST\n ");
printf( " <Server> is the server to POST to\n ");
printf( " <Path> is the virtual path to POST to\n ");
exit(0);
}

if ( ((dwPostSize = strtoul(argv[1],NULL,10)) == 0) || (dwPostSize > = 2047999) )
{
printf( "%s is invalid size. Valid sizes are from 1 to 2047999\n ", argv[1]);
exit(0);
}

printf( "Test of POSTing %luKB with WinInet\n ", dwPostSize);

dwPostSize *= 1024; // Convert KB to bytes

HINTERNET hSession = InternetOpen( "HttpSendRequestEx ", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if(!hSession)
{
printf( "Failed to open session\n ");
exit(0);
}


HINTERNET hConnect = InternetConnect(hSession, argv[2], INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP,NULL, NULL);
if (!hConnect)
printf( "Failed to connect\n " );
else
{
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST ", argv[3],
NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (!hRequest)
printf( "Failed to open request handle\n " );
else
{
if(UseHttpSendReqEx(hRequest, dwPostSize))
{
char pcBuffer[BUFFSIZE];
DWORD dwBytesRead;

printf( "\nThe following was returned by the server:\n ");
do
{dwBytesRead=0;
if(InternetReadFile(hRequest, pcBuffer, BUFFSIZE-1, &dwBytesRead))
{
pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer
printf( "%s ", pcBuffer);
}
else
printf( "\nInternetReadFile failed ");
}while(dwBytesRead> 0);
printf( "\n ");
}
if (!InternetCloseHandle(hRequest))
printf( "Failed to close Request handle\n " );
}
if(!InternetCloseHandle(hConnect))
printf( "Failed to close Connect handle\n ");
}
if( InternetCloseHandle( hSession ) == FALSE )
printf( "Failed to close Session handle\n " );

printf( "\nFinished.\n " );
}


BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize)
{
INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
int n;
BYTE pBuffer[1024];
BOOL bRet;

BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;

if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0))


{
printf( "Error on HttpSendRequestEx %d\n ",GetLastError() );
return FALSE;
}

FillMemory(pBuffer, 1024, 'D '); // Fill buffer with data

bRet=TRUE;
for(n=1; n <=(int)dwPostSize/1024 && bRet; n++)
{
if(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten))
printf( "\r%d bytes sent. ", n*1024);
}

if(!bRet)
{
printf( "\nError on InternetWriteFile %lu\n ",GetLastError() );
return FALSE;
}

if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n ", GetLastError());
return FALSE;
}

return TRUE;
}


[解决办法]
获取网页内容我知道,但是不知道怎么post,请高手指点.

获取网页内容:

在窗口上放一个 NMHTTP1
再放一个Memo1

void __fastcall TFormMain::Button1Click(TObject *Sender)
{
NMHTTP1-> TimeOut = 5000;
NMHTTP1-> InputFileMode = false;
NMHTTP1-> OutputFileMode = false;
NMHTTP1-> ReportLevel = Status_Basic;

NMHTTP1-> Get( "http://www.aeasy.com ");
Memo1-> Text = NMHTTP1-> Body;
}

热点排行