indy10idhttpc++builder-10-seattle

IdHTTP: EIdException ResponseCode (Colorize + Replace)


I have two problems here. So I need your help.

  1. The result of microsoft.com's response code is some time HTTP/1.1 403 Forbidden or HTTP/1.1 200 OK.`

    try 
    {
        IdHTTP->Get("http://www.microsoft.com");
        ListBox->Items->Add(IdHTTP->Response->ResponseCode);
    }
    catch (const EIdException &E) 
    {
        ListBox->Items->Add(E.Message);
        ListBox->Items->Add(IdHTTP->Response->ResponseText);
    }
    

    But when I checked it on http://web-sniffer.net/ or http://tools.seobook.com/server-header-checker then its results is HTTP/1.1 302 Moved Temporarily.

    Why the results from IdHTTP is different from the both url above?. How can IdHTTP achieve the same http status code?.

  2. Colorize & replace E.Message error of EIdException / Exception in TListBox.

    For example, I want to replace the "Socket Error # 10061Connection refused" with "your connection is refused".

    ListBox->Items->Add(StringReplace(E.Message,"Socket Error # 10061Connection refused.","your connection is refused.",TReplaceFlags()<<rfReplaceAll));
    

    But using that way, the result is still same.

Thanks for taking the time to read this. Any help or suggestions with would be greatly appreciated!!


Solution

  • Outside of site maintenance, I doubt http://www.microsoft.com would ever return a 403 Forbidden error under normal conditions. But, like any site, I suppose it could happen at times. It is a fatal error, you cannot access the URL at that time (if at all).

    302 Moved Temporarily is an HTTP redirect. The TIdHTTP.OnRedirect event will be triggered to give you the new URL. If the TIdHTTP.HandleRedirects property is true, or the OnRedirect event handler returns true, TIdHTTP will handle the redirect internally and automatically request the new URL for you. TIdHTTP.Get() does not exit until the final URL is reached, or an error occurs.

    As for error handling, if you want to customize the message you display based on the type of error that occurred, you need to actually differentiate between the various types of errors that can occur, eg:

    try 
    {
        IdHTTP->Get("http://www.microsoft.com");
        ListBox->Items->Add(IdHTTP->Response->ResponseCode);
    }
    catch (const EIdHTTPProtocolException &E) 
    {
        // HTTP error
        // E.ErrorCode contains the ResponseCode
        // E.Message contains the ResponseText
        // E.ErrorMessage contains the content of the error body, if any
    
        ListBox->Items->Add(E.Message);
    }
    catch (const EIdSocketError &E)
    {
        // Socket error
        // E.LastError contains the socket error code
        // E.Message contains the socket error message
    
        if (E.LastError == Id_WSAECONNREFUSED)
            ListBox->Items->Add("Your connection is refused");
        else
            ListBox->Items->Add(E.Message);
    }
    catch (const EIdException &E) 
    {
        // any other Indy error
        ListBox->Items->Add(E.Message);
    }
    catch (const Exception &E) 
    {
        // any other non-Indy error
        ListBox->Items->Add(E.Message);
    }