Sign in to follow this  
Followers 0
Unc3nZureD

C++ equal of InetRead()

6 posts in this topic

I'm working on a DLL which should read a really simple string from a website.

The link looks like this: http://genericwebsite.cu.cc/script.php?license=123

The text it prints out is a simple true or false, no more.

How could I do this? I'm really newbie in C++ :) I'm using VC++ 2013 (express)

Share this post


Link to post
Share on other sites



Could anyone answer me please? :)

Share this post


Link to post
Share on other sites

Oh, cool, that's what I need :P Thanks.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I'm glad I could help you. For all others interested in this I wrote the following function. Have fun!

#include <Windows.h>
#include <tchar.h>
#include <Wininet.h>

#pragma comment(lib,"Wininet.lib")

BOOL InetRead(const TCHAR* sUrl, char* buffer, DWORD* dwBufLen,
                TCHAR* sProxy = NULL, TCHAR* sProxyUser = NULL, TCHAR* sProxyPwd = NULL);

BOOL InetRead(const TCHAR* sUrl, char* buffer, DWORD* dwBufLen, TCHAR* sProxy, TCHAR* sProxyUser, TCHAR* sProxyPwd)
{
    HINTERNET hOpen = NULL;
    HINTERNET hFile = NULL;
    DWORD dwBytesRead = 0, dwAllBytesRead = 0;
    DWORD dwCode, dwParamLen = 4;

    TCHAR* agent = _T("AutoC");
    hOpen = InternetOpen(agent, INTERNET_OPEN_TYPE_PRECONFIG, sProxy, NULL, 0);
    if (!hOpen)
        goto err1;

    hFile = InternetOpenUrl(hOpen, sUrl, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, NULL);
    if (!hFile)
        goto err2;

    HttpSendRequest(hFile, NULL, 0, NULL, 0);
    HttpQueryInfo(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwParamLen, NULL);

    if (dwCode == HTTP_STATUS_PROXY_AUTH_REQ)
    {
        if ((sProxy != NULL) && (sProxyUser != NULL) && (sProxyPwd != NULL))
        {
            InternetSetOption(hFile, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)sProxyUser, lstrlen(sProxyUser));
            InternetSetOption(hFile, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)sProxyPwd, lstrlen(sProxyPwd));
            HttpSendRequest(hFile, NULL, 0, NULL, 0);
            HttpQueryInfo(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwCode, &dwParamLen, NULL);
        }
        else
            goto err3;
    }

    do {
        InternetReadFile(hFile, (LPVOID)(buffer + dwAllBytesRead), *dwBufLen - dwAllBytesRead - 1, &dwBytesRead);
        dwAllBytesRead += dwBytesRead;
    } while ((dwBytesRead) && (dwAllBytesRead < *dwBufLen));

    buffer[dwAllBytesRead] = 0; // because last call of InternetReadFile may add garbage!!
    goto OK;

err3:   InternetCloseHandle(hFile);
err2:   InternetCloseHandle(hOpen);
err1:   *dwBufLen = 0;
    return FALSE;

OK: InternetCloseHandle(hFile);
    InternetCloseHandle(hOpen);
    *dwBufLen = dwAllBytesRead;
    return TRUE;
}

int APIENTRY _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    char sBuffer[20000];
    DWORD dwBuferLen = sizeof sBuffer;
    TCHAR *sUrl = _T("http://www.google.com/");

    if (!InetRead(sUrl, sBuffer, &dwBuferLen))//, _T("http://proxy.com"), _T("user"), _T("password")))
    {
        MessageBox(NULL, _T("InetRead failed!"), _T("Error"), MB_ICONERROR);
        return 1;
    }

#ifdef _UNICODE
    int len = MultiByteToWideChar(CP_ACP, 0, sBuffer, -1, NULL, 0);
    wchar_t* sNew = (wchar_t*)malloc(len * (sizeof(wchar_t)));
    MultiByteToWideChar(CP_ACP, 0, sBuffer, -1, sNew, len);
    sNew[len - 1] = 0;

    MessageBox(NULL, sNew, sUrl, MB_OK);
    free(sNew);

#else

    MessageBox(NULL, sBuffer, sUrl, MB_OK);
#endif

    return 0;
}

Edit: Added proxy authentication ;)

Edited by funkey
1 person likes this

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

I tried the AutoIt function InetRead and I'm not sure if this is a bug. In AutoIt I have following code:

HTTpSetProxy(2, "192.168.100.201", "user", "password")
Global $bData = InetRead("http://www.google.com/")
Global $nBytesRead = @extended
If @error Then ConsoleWrite("Error InetRead " & @error & @LF)
Global $sData = BinaryToString($bData)

MsgBox(4096, "InetRead AutoIt", "Bytes read: " & $nBytesRead & " - " & StringLen($sData) & @CRLF & @CRLF & $sData)

This seems to work but I get code from proxy page not from Google. If I add correct port to the proxy address ("192.168.100:3128") then I get about 12000 bytes read but not text and error is set to 13. What does this mean?

When I do the same with my C-function I always get text from the Google page, no need to specify the proxy port.

char sBuffer[20000];
    DWORD dwBufferLen = sizeof sBuffer;
    TCHAR *sUrl = _T("http://www.google.com/");

    if (!InetRead(sUrl, sBuffer, &dwBuferLen, _T("192.168.100.201"), _T("user"), _T("password")))
    {
        MessageBox(NULL, _T("InetRead failed!"), _T("InetRead AutoC"), MB_ICONERROR);
        return 1;
    }

    TCHAR sBytes[64];
    _stprintf_s(sBytes, _T("Bytes read: %i"), dwBufferLen); // 11584 bytes now
    MessageBox(NULL, sBytes, _T("InetRead AutoC"), MB_ICONINFORMATION);

Maybe a dev can have a look into AutoIt's code.

Greetings

funkey


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0