?
Solved

How to use wininet with vc6?

Posted on 2006-07-20
11
Medium Priority
?
1,516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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!

 
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
 
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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 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