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

Posted on 2007-10-17
Last Modified: 2012-06-27
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

Question by:Mobinex
    LVL 86

    Accepted Solution

    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);
    LVL 1

    Author Comment

    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?


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    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.

    755 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

    21 Experts available now in Live!

    Get 1:1 Help Now