18,356
社区成员
发帖
与我相关
我的任务
分享
STDMETHODIMP Cweb::Send(VARIANT server, VARIANT header, VARIANT *ret)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
// TODO: Add your implementation code here
WORD wVersionRequested;
WSADATA wsaData;
int err;
char* str=_com_util::ConvertBSTRToString(server.bstrVal);
char* head=_com_util::ConvertBSTRToString(header.bstrVal);
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( 0 != err ) //检查Socket初始化是否成功
{
ret->vt=VT_BSTR;
ret->bstrVal=SysAllocString(L"Socket2.0初始化失败,Exit!");
return 0;
}
int sock,timeout;
char *hosttest;
sockaddr_in addr_recFrom;
sockaddr *temp_sockaddr;
hostent *test;
sock=socket(AF_INET,SOCK_STREAM,0);
test=gethostbyname(str);
hosttest=inet_ntoa(*((struct in_addr *)test->h_addr));
memset(&addr_recFrom,0,sizeof(addr_recFrom));
addr_recFrom.sin_family = AF_INET;
addr_recFrom.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *)test->h_addr)));
addr_recFrom.sin_port = htons(80);
int flag;
timeout=10;
setsockopt(sock,SOL_SOCKET,SOCK_STREAM,reinterpret_cast<char FAR *>(&timeout),sizeof(int));
temp_sockaddr=new sockaddr;
memset(temp_sockaddr,0,sizeof(temp_sockaddr));
temp_sockaddr=(struct sockaddr *)&addr_recFrom;
flag=connect(sock,(struct sockaddr *)&addr_recFrom,sizeof(addr_recFrom));
send(sock, head, strlen(head), 0);
char text[1024]="";
CString ss;
while ( recv(sock, text, 1024, 0) > 0)
{
CString s;
s.Format("%s",text);
ss+= s;
memset(text,0,1024);
}
ret->vt=VT_BSTR;
ret->bstrVal=ss.AllocSysString();
closesocket(sock);
WSACleanup();
return S_OK;
}
#include<wininet.h>
#include<process.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#pragma comment(lib,"wininet.lib")
#include<stdlib.h>
#define UNICODE
#define _UNICODE
#include<crtdbg.h>
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
#define TIMEOUT 3000
void __stdcall Callback(HINTERNET hInternet,
DWORD dwContext,
DWORD dwInternetStatus,
LPVOID lpStatusInfo,
DWORD dwStatusInfoLen);
CString ToString (DWORD i)
{
CString str;
str.Format("%d",i);
return str;
}
HINTERNET hSession;
HINTERNET hFile;
HANDLE hComplete;
HANDLE CloseFile;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CoInitialize(0);
_ConnectionPtr pConnection;
_RecordsetPtr pRecordset;
if(pConnection.CreateInstance(__uuidof(Connection))!=S_OK)
{
return false;
}
CString strConnect;
CString strDBFile="1.mdb";
strConnect.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"),strDBFile);
if(pConnection->Open(_bstr_t( strConnect),"", "", 0)==S_OK)
{
// _RecordsetPtr pRecordset;
if(pRecordset.CreateInstance(__uuidof(Recordset)) != S_OK)
{
pConnection->Close();
return false;
}
CString strSql;
strSql="select * from db where flag=false order by id";
HRESULT hr=pRecordset->Open(_bstr_t(strSql),pConnection.GetInterfacePtr(), adOpenStatic,
adLockOptimistic,
adCmdText);
if(hr!=S_OK)
{
pConnection->Close();
return false;
}
}
hComplete=CreateEvent(NULL,FALSE,FALSE,NULL);
CloseFile=CreateEvent(NULL,FALSE,FALSE,NULL);
hSession = InternetOpen(NULL,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_ASYNC);
if (InternetSetStatusCallback(hSession,
(INTERNET_STATUS_CALLBACK)&Callback) == INTERNET_INVALID_STATUS_CALLBACK)
{
cout<<"回调函数注册失败"<<endl;
return false;
}
if(!pRecordset->BOF)
{
pRecordset->MoveFirst();
while(!pRecordset->adoEOF)
{
ResetEvent(hComplete);
ResetEvent(CloseFile);
CString url=(LPCSTR)(_bstr_t)pRecordset->Fields->GetItem("url")->Value;
ULONG l=INTERNET_CONNECTION_LAN;
printf("%s\n",url.GetBuffer(0));
url.ReleaseBuffer();
hFile =InternetOpenUrl(hSession,url,NULL,0,INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD,1);
if(WaitForSingleObject(hComplete,TIMEOUT)==WAIT_TIMEOUT)
{
printf("超时\n");
}
else
{
CString str;
bool bAllDone=false;
do
{
char lpReadBuff[512];
INTERNET_BUFFERS InetBuff;
bool outed=false;
ResetEvent(hComplete);
memset(lpReadBuff,0,512);
FillMemory(&InetBuff, sizeof(InetBuff), 0);
InetBuff.dwStructSize = sizeof(InetBuff);
InetBuff.lpvBuffer = lpReadBuff;
InetBuff.dwBufferLength = sizeof(lpReadBuff) - 1;
BOOL bOk=InternetReadFileEx(hFile,
&InetBuff,
IRF_ASYNC, 1);
if(!bOk&&GetLastError()==ERROR_IO_PENDING)
{
if(WaitForSingleObject(hComplete,2000)==WAIT_TIMEOUT)
{
printf("error\n");
}
}
// cout<<lpReadBuff<<endl;
CString s;
s.Format("%s",lpReadBuff);
if(s=="")
break;
str+=s;
lpReadBuff[InetBuff.dwBufferLength] = 0;
// cout<<InetBuff.dwBufferLength <<endl;
if (InetBuff.dwBufferLength == 0)
{
bAllDone = TRUE;
}
}while(bAllDone==false);
pRecordset->PutCollect("result",_variant_t(str));
COleDateTime time=COleDateTime::GetCurrentTime();
pRecordset->PutCollect("times2",_variant_t(time));
pRecordset->PutCollect("flag",_variant_t(true));
pRecordset->Update();
}
InternetCloseHandle(hFile);
hFile=NULL;
WaitForSingleObject(CloseFile,2000);
// cout<<GetLastError()<<endl;
pRecordset->MoveNext();
}
}
if(pConnection)
{
pConnection->Close();
pConnection.Release();
}
if(pRecordset)
{
pRecordset.Release();
}
}
InternetSetStatusCallback(hSession,NULL);
InternetCloseHandle(hSession);
CoUninitialize();
system("pause");
return nRetCode;
}
void __stdcall Callback(HINTERNET hInternet,
DWORD dwContext,
DWORD dwInternetStatus,
LPVOID lpStatusInfo,
DWORD dwStatusInfoLen)
{
// cout<<dwInternetStatus<<endl;
switch(dwInternetStatus)
{
case INTERNET_STATUS_RESOLVING_NAME:
case INTERNET_STATUS_HANDLE_CREATED:
{
INTERNET_ASYNC_RESULT* res = (INTERNET_ASYNC_RESULT*)lpStatusInfo;
hFile = (HINTERNET)(res->dwResult);
}
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
while(!SetEvent(hComplete))
{
printf("%d",GetLastError());
}
break;
case INTERNET_STATUS_HANDLE_CLOSING:
while(!SetEvent(CloseFile))
{
printf("%d",GetLastError());
}
break;
case INTERNET_STATUS_CONNECTION_CLOSED:
break;
}
return;
}