Pelles C forum
C language => Beginner questions => Topic started by: Grincheux on March 23, 2017, 06:12:40 PM
-
I try to make a search on many web search engines.
I need your help to understand how I must do it.
I join my project
PCTSTR rgpszAcceptTypes[] = {
_T("text/*"),
_T("video/*"),
_T("image/*"),
_T("audio/*"),
NULL
} ;
char szEngineTitle[] = _T("My HTTP Search Engine") ;
char szGet[] = _T("GET") ;
char szBingSearch[] = _T("search?q=%s") ;
char szBingEngine[] = _T("http://www.bing.com") ;
LPSTR Search_OnTheWeb(LPSTR __lpszEngine,LPSTR __lpszVerb,LPDWORD __lpResultLength)
{
HINTERNET _hInternet, _hConnect, _hRequest ;
DWORD _dwContentLen, _dwBufLen, _cReadCount, _dwBytesRead, _dwReadSize ;
DWORD _dwError ;
LPSTR _lpData, _lpCopyPtr ;
_hInternet = _hConnect = _hRequest = NULL ;
_dwReadSize = _dwContentLen = _cReadCount = _dwBytesRead = _dwBufLen = _dwError = 0 ;
_lpData = _lpCopyPtr = NULL ;
*__lpResultLength = 0 ;
if(__lpszEngine)
{
if(__lpszVerb)
{
if(__lpResultLength)
{
_hInternet = InternetOpen(szEngineTitle,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0) ;
if(_hInternet)
{
_hConnect = InternetConnect(_hInternet,__lpszEngine,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0) ;
if(_hConnect)
{
_hRequest = HttpOpenRequest(_hConnect,szGet,__lpszVerb,NULL,NULL,rgpszAcceptTypes,INTERNET_FLAG_RELOAD,0) ;
if(_hRequest)
{
if(HttpSendRequest(_hRequest,NULL,0,NULL,0))
{
_dwBufLen = sizeof(_dwContentLen) ;
if(!HttpQueryInfo(_hRequest,HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER,(LPVOID)&_dwContentLen,&_dwBufLen,0))
{
_dwError = GetLastError() ;
if(_dwError == ERROR_HTTP_HEADER_NOT_FOUND)
{
InternetCloseHandle(_hRequest) ;
InternetCloseHandle(_hInternet) ;
InternetCloseHandle(_hConnect) ;
return (NULL) ;
}
else
{
if(_dwError == ERROR_INSUFFICIENT_BUFFER)
{
_lpData = (LPSTR) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_dwContentLen + 1) ;
if(_lpData)
{
// We will read 10% of data with each read.
_dwReadSize = _dwContentLen / 10 ;
_lpCopyPtr = _lpData ;
for(_cReadCount = 0 ; _cReadCount < 10 ; _cReadCount++)
{
InternetReadFile(_hRequest,_lpCopyPtr,_dwReadSize,&_dwBytesRead) ;
_lpCopyPtr = _lpCopyPtr + _dwBytesRead ;
}
// Extra read to account for integer division round off
InternetReadFile(_hRequest,_lpCopyPtr,_dwContentLen - (_lpCopyPtr - _lpData),&_dwBytesRead) ;
// Null terminate data
_lpData[_dwContentLen] = 0 ;
*__lpResultLength = _dwContentLen ;
}
}
}
}
InternetCloseHandle(_hRequest) ;
}
InternetCloseHandle(_hInternet) ;
}
InternetCloseHandle(_hConnect) ;
}
}
}
}
}
return (_lpData) ;
}
LPSTR Search_Bing(LPSTR __lpszString,LPDWORD __lpResultLength)
{
char _szTmp[1024] ;
wsprintf(_szTmp,szBingSearch,__lpszString) ;
return (Search_OnTheWeb(szBingEngine,_szTmp,__lpResultLength)) ;
}
LPSTR Search_Google(LPSTR __lpszString,LPDWORD __lpResultLength)
{
char _szTmp[1024] ;
wsprintf(_szTmp,szGoogleSearch,__lpszString) ;
return (Search_OnTheWeb(szGoogleEngine,_szTmp,__lpResultLength)) ;
}
The problem is at the line : HttpSendRequest(_hRequest,NULL,0,NULL,0)
Could you help me please?
Thanks
-
INFO: WinInet Error Codes (https://support.microsoft.com/en-us/help/193625/info-wininet-error-codes-12001-through-12156)
12005 ERROR_INTERNET_INVALID_URL The URL is invalid.
char szBingEngine[] = _T("www.bing.com") ;
FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,
GetModuleHandle("WinInet.dll"), _dwError, 0, szTmp, sizeof(szTmp), NULL);
-
You are right, but HttQueryInfo always returns 0.
GetLasError returns ERROR_HTTP_HEADER_NOT_FOUND.
I don't know what to do.
Help is URGENT. >:( >:( >:( >:( >:(
-
When I take a look with fidler I have :
Request Count: 1
Bytes Sent: 177 (headers:177; body:0)
Bytes Received: 44 491 (headers:637; body:43 854)
ACTUAL PERFORMANCE
--------------
ClientConnected: 16:55:10.018
ClientBeginRequest: 16:55:10.159
GotRequestHeaders: 16:55:10.159
ClientDoneRequest: 16:55:10.159
Determine Gateway: 0ms
DNS Lookup: 34ms
TCP/IP Connect: 42ms
HTTPS Handshake: 0ms
ServerConnected: 16:55:10.237
FiddlerBeginRequest: 16:55:10.237
ServerGotRequest: 16:55:10.237
ServerBeginResponse: 16:55:10.362
GotResponseHeaders: 16:55:10.362
ServerDoneResponse: 16:55:10.986
ClientBeginResponse: 16:55:10.986
ClientDoneResponse: 16:55:10.986
Overall Elapsed: 0:00:00.826
So there are bytes sent to me!
-
Search page don't have a Content length.