Solved

How to use wininet with vc6?

Posted on 2006-07-20
11
1,500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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