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

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

HINTERNET hConn =  InternetConnect (
                hOpen,                      // InternetOpen handle
                "www.experts-exchange.com",             // 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;
    InternetGetLastResponseInfo(&dwError,buf,1024);

    MessageBox(NULL,buf,_T("InternetConnect() failed"), MB_OK);
}
0
 
MobinexAuthor Commented:
But for server URL:   "www.experts-exchange.com"
Now, I replace to any URL as: www.testtest.com,   test, www.test.com.cn

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?

0
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.