Solved

How to use wininet with vc6?

Posted on 2006-07-20
11
1,498 Views
Last Modified: 2008-01-09
Hi,
Im having problem with wininet in visual c++ 6.
I need to connect to internet in my win32 project (not MFC), i understand that it require wininet to do that.
I have added wininet.lib and wininet.h into my project, everything is running fine but i cant figure out why my code fail to open a page in the internet.
It can ping an internet address but fail to open page.
Can anyone tell me what mistake that i make? here is the code that i use:


    DWORD inetResult;

    inetResult=InternetAttemptConnect(0);

    if (inetResult == ERROR_SUCCESS)
    {
        MessageBox(NULL, L"Inet Success", L"Success", MB_OK);

        bool pingResult;

        pingResult=InternetCheckConnection(L"http://www.griffinbox.org", FLAG_ICC_FORCE_CONNECTION, 0);

        if (pingResult == true)
        {
            MessageBox(NULL, L"Ping Success", L"Success", MB_OK);

            HINTERNET inetOpen;
            inetOpen=InternetOpen(L"HTTPGET", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);

            if (inetOpen==NULL)
            {
                MessageBox(NULL, L"Open Fail", L"Fail", MB_OK);
            }
            else
            {
                MessageBox(NULL, L"Open Success", L"Connected", MB_OK);


                HINTERNET inetConnect;

                inetConnect=InternetConnect(inetOpen, L"griffinbox.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

                if (inetConnect==NULL)
                {
                    MessageBox(NULL, L"Connection Fail", L"Fail", MB_OK);
                }
                else
                {
                    MessageBox(NULL, L"Connection Success", L"Connected", MB_OK);

                    LPCTSTR *acceptTypes = NULL;


                    HINTERNET inetSession;
                    inetSession=HttpOpenRequest(inetConnect, L"GET", L"/index.php",
                                                NULL, NULL, acceptTypes, INTERNET_FLAG_EXISTING_CONNECT, 0);



                    if (inetSession==NULL)
                    {
                        MessageBox(NULL, L"Session Fail", L"Fail", MB_OK);
                    }
                    else
                    {
                        MessageBox(NULL, L"Session Success", L"Connected", MB_OK);


                        if (HttpSendRequest(inetSession, NULL, 0, NULL, 0))
                        {
                            MessageBox(NULL, L"Request Sent", L"Request Sent", MB_OK);


                            LPTSTR dataBuffer;
                            DWORD  dwSize;       // size of the data available
                            DWORD  dwDownloaded; // size of the downloaded data

                            do
                            {
                                if (InternetQueryDataAvailable(inetSession,&dwSize,0,0))
                                {
                                    if (dwSize > 0)
                                    {
                                        MessageBox(NULL, L"Its opened!", L"hore!!", MB_OK);
                                    }
                                    dataBuffer = new TCHAR[dwSize+1];

                                    if (InternetReadFile(inetSession, (LPVOID)dataBuffer, dwSize, &dwDownloaded))
                                    {
                                        dataBuffer[dwDownloaded]='\0';

                                        MessageBox(NULL, (LPTSTR)dataBuffer, L"hehe", MB_OK);
                                    }

                                    delete[] dataBuffer;

                                    if (dwDownloaded == 0)
                                        break;
                                }
                                else
                                {
                                    break;
                                }
                            }while (true);

                                               
                        }
                        else
                        {
                            MessageBox(NULL, L"Request Fail", L"Request Fail", MB_OK);
                        }

                        bool inetClose;
                        inetClose=InternetCloseHandle(inetSession);

                        if (inetClose==true)
                        {
                            MessageBox(NULL, L"Session Closed Successfully", L"Close Success", MB_OK);
                        }
                        else
                        {
                            MessageBox(NULL, L"Session Close Failed", L"Close Failed", MB_OK);
                        }
                   
                    }

                }

            }

        }
        else
        {
            MessageBox(NULL, L"Ping Fail", L"Fail", MB_OK);
        }

    }
    else
    {
        MessageBox(NULL, L"Inet Fail", L"Fail", MB_OK);
    }

0
Comment
Question by:iphone
11 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17145114
Have you tried to get error using GetLastError() ?

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17145348
you may check for different WinInet errors here :http://support.microsoft.com/?kbid=193625

-MAHESH
0
 
LVL 30

Expert Comment

by:Axter
ID: 17145559
I recommend you installed the latest psdk (Platform SDK), which you can download from MS.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17146559
Stupid Q - have you tried

inetConnect=InternetConnect(inetOpen, L"www.griffinbox.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

?
0
 

Author Comment

by:iphone
ID: 17151373
Yes i use GetLastError, and this is the error message it gave me
HttpSendRequest failed with error 997: Overlapped I/O operation is in progress

and jkr i've tried this:
inetConnect=InternetConnect(inetOpen, L"www.griffinbox.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
but still no use, have you try the whole code yourself?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 22

Expert Comment

by:mahesh1402
ID: 17151844
btw do you use some kind of proxy or something to connect to Internet ?

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17151889
You are trying to to send the request asynchronously as you have mentioned the INTERNET_FLAG_ASYNC in InternetOpen. You need to define and set a callback function for the same. Sending requests asynchronously has a lot of bindings with respect to handling the above errors and the proper sequence of requests and corresponding notifications.

Refer to http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q275046 <== download asynchttp.exe..Refer asynchttp.cpp

Above explains the correct steps for sending a request asynchronously. Refer 'Asynchttp.cpp'..A callback function is created and set using InternetSetStatusCallback(). IN call back function CallBack appropriate event is set depending on Internet status such as connect, open, send etc. Refer full src file and your will get it how to do it.

-MAHESH
0
 

Author Comment

by:iphone
ID: 17151917
I see, im not sure why i use asynchronous connection at the first place, what flag do you use to connect to internet normally?and what is asynchronous connection?
I just want to do a simple internet connection that open a URL and retrieve the HTML from that, just as simple as that.
May i ask you to highligh which connection flag to use?
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 500 total points
ID: 17151930
Other wise this simple solution for you and above works with this :

If you want to send the request synchronously, try replacing the INTERNET_FLAG_ASYNC by 0.

just modify this :

           HINTERNET inetOpen;
            inetOpen=InternetOpen("HTTPGET", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); // Replacing the INTERNET_FLAG_ASYNC by 0.

try it

-MAHESH
0
 

Author Comment

by:iphone
ID: 17151946
wow it works like magic, you're the best mahesh, thx a lots,
anyway may i know what is asynchronous connection for?
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17151980
Asynchronous Http is similar to regular http, except that they are capable of serving many client requests..above sample on MSDN with callback function is example of asynchronous http request..

-MAHESH
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now