How to handle error InternetConnect as connection timeout, server not found by using HTTP with wininet

Hi Experts,
I have used HTTP connection in C++ as ::InternetOpen, ::InternetConnect to open and connect to server URL
BUT I don't know how to handle connection error as page not found, server not available,...
Because with my testing I use to connect with any URL, a invalide URL, a unavailble URL, I see InternetConnect return a NOT NULL connection.

So how can I know when server is not available, or server not found, or connect timeout,....
Can you give me a handle to handle errors by using HTTP with wininet

Who is Participating?
jkrConnect With a Mentor Commented:
Usually, you'd do that like

HINTERNET hConn =  InternetConnect (
                hOpen,                      // InternetOpen handle
                "",             // Server  name
      INTERNET_DEFAULT_HTTP_PORT,// Default HTTP port - 80
                "",                         // User name
                "",                         //  User password
                INTERNET_SERVICE_HTTP,      // Service
      0,                          // Flags
      0                           // Context

if (hConn == NULL) { // error

    TCHAR buf[1024];
    DWORD dwError;

    MessageBox(NULL,buf,_T("InternetConnect() failed"), MB_OK);
MobinexAuthor Commented:
But for server URL:   ""
Now, I replace to any URL as:,   test,

It alway return NOT NULL, it means we can create connection.

So, when it will be NULL connection created?
And to handle server errors we will do at the time we send request?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.