Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1534
  • Last Modified:

How to use wininet with vc6?

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
iphone
Asked:
iphone
1 Solution
 
mahesh1402Commented:
Have you tried to get error using GetLastError() ?

-MAHESH
0
 
mahesh1402Commented:
you may check for different WinInet errors here :http://support.microsoft.com/?kbid=193625

-MAHESH
0
 
AxterCommented:
I recommend you installed the latest psdk (Platform SDK), which you can download from MS.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
Stupid Q - have you tried

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

?
0
 
iphoneAuthor Commented:
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
 
mahesh1402Commented:
btw do you use some kind of proxy or something to connect to Internet ?

-MAHESH
0
 
mahesh1402Commented:
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
 
iphoneAuthor Commented:
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
 
mahesh1402Commented:
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
 
iphoneAuthor Commented:
wow it works like magic, you're the best mahesh, thx a lots,
anyway may i know what is asynchronous connection for?
0
 
mahesh1402Commented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now