NO

Author Topic: Web Search  (Read 2568 times)

Grincheux

  • Guest
Web Search
« 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

Code: [Select]
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 :
Code: [Select]
HttpSendRequest(_hRequest,NULL,0,NULL,0)
Could you help me please?

Thanks
« Last Edit: March 23, 2017, 06:15:33 PM by Grincheux »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Web Search
« Reply #1 on: March 24, 2017, 03:28:14 PM »
INFO: WinInet Error Codes
12005       ERROR_INTERNET_INVALID_URL               The URL is invalid.
Code: [Select]
char szBingEngine[] = _T("www.bing.com") ;
Code: [Select]
FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,
GetModuleHandle("WinInet.dll"), _dwError, 0, szTmp, sizeof(szTmp), NULL);
« Last Edit: March 24, 2017, 03:58:59 PM by TimoVJL »
May the source be with you

Grincheux

  • Guest
Re: Web Search
« Reply #2 on: March 24, 2017, 04:50:36 PM »
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. >:( >:( >:( >:( >:(

Grincheux

  • Guest
Re: Web Search
« Reply #3 on: March 24, 2017, 05:00:22 PM »
When I take a look with fidler I have :

Code: [Select]
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!

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Web Search
« Reply #4 on: March 24, 2017, 08:10:05 PM »
Search page don't have a Content length.
May the source be with you