Visual C++ Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Visual C++ Examples

CkString
Byte Array
Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
Encryption
FTP
HTML-to-XML
HTTP
IMAP
MHT / HTML Email
RSA Encryption
S/MIME
Socket
Spider
Tar
Upload
XML
XMP
Zip Compression


More Examples...
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

 

HTTP Redirect Handling

Examine HTTP redirects.

Download Chilkat C++ Libraries for VC++ 8.0 / Win32

Download Chilkat C++ Libraries for VC++ 7.0 / Win32

Download Chilkat C++ Libraries for VC++ 6.0 / Win32

#include <CkHttp.h>
#include <CkString.h>

void ChilkatSample(void)
    {
    CkHttp http;

    bool success;

    //  Any string unlocks the component for the 1st 30-days.
    success = http.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        printf("%s\n",http.lastErrorText());
        return;
    }

    CkString url;
    CkString html;
    int status;

    url = "http://www.planyourweddingonline.co.za/";

    //  The FollowRedirects property controls whether redirects
    //  are automatically followed.  The default behavior is to
    //  automatically follow redirects.

    //  Explicitly set FollowRedirects so that redirects are automatically taken:
    http.put_FollowRedirects(true);

    //  Send the HTTP GET and return the content in a string.
    html = http.quickGetStr(url);
    if (html.getNumChars() == 0 ) {
        printf("%s\n",http.lastErrorText());
    }

    //  On success, LastErrorText will provide information about
    //  what happened during the call.
    printf("--------------- LastErrorText ------------------\n");
    printf("%s\n",http.lastErrorText());
    printf("------------------------------------------------\n");

    //  In this case, we see something like this:
    //  ChilkatLog:
    //    QuickGetHtml:
    //      DllDate: Jul 27 2007
    //      url: http://www.planyourweddingonline.co.za/
    //      httpServer: www.planyourweddingonline.co.za
    //      port: 80
    //      StatusCode: 302
    //      StatusText: Found
    //      Reading chunked response
    //      redirectUrl: main/main/home/index.php
    //      url: http://www.planyourweddingonline.co.za/main/main/home/index.php
    //      StatusCode: 302
    //      StatusText: Found
    //      Reading chunked response
    //      redirectUrl: /main/main/home/index.php?SMC=1
    //      url: http://www.planyourweddingonline.co.za/main/main/home/index.php?SMC=1
    //      StatusCode: 200
    //      StatusText: OK
    //      CompressedSize: 7434
    //      UncompressedSize: 40999

    //  Was the GET redirected?
    if (http.get_WasRedirected() == true) {
        printf("Chilkat HTTP followed the redirect.\n");

        //  Display the final redirect URL:
        printf("Final URL:\n");
        printf("%s\n",http.finalRedirectUrl());

        //  Note the HTML returned is from the final redirect URL.

    }
    else {
        printf("Not redirected.\n");
    }

    status = http.get_LastStatus();
    if (status == 200) {
        printf("status = 200, OK!\n");
    }
    else {
        printf("HTTP Response status = %d\n"
            ,status);

        //  Display the complete response header.
        printf("%s\n",http.lastResponseHeader());
    }

    //  Now try it without following redirects:
    printf("-------- Now trying without following redirects....\n");
    http.put_FollowRedirects(false);

    //  Send the HTTP GET and return the content in a string.
    html = http.quickGetStr(url);
    if (html.getNumChars() == 0 ) {
        //  the HTML string can NULL if a 302 redirect response is received.
        printf("HTML string returned NULL...\n");
    }

    //  On success, LastErrorText will provide information about
    //  what happened during the call.
    printf("--------------- LastErrorText ------------------\n");
    printf("%s\n",http.lastErrorText());
    printf("------------------------------------------------\n");

    //  In this case, we see something like this:
    //  ChilkatLog:
    //    QuickGetHtml:
    //      DllDate: Jul 27 2007
    //      url: http://www.planyourweddingonline.co.za/
    //      StatusCode: 302
    //      StatusText: Found
    //      Reading chunked response
    //      redirectUrl: main/main/home/index.php

    //  Was this a redirect?  Even if FollowRedirects is false,
    //  WasRedirected will be true (non-zero) if the response
    //  indicated a redirect.
    if (http.get_WasRedirected() == true) {
        printf("This was a redirect response\n");

        //  When redirects are not followed, FinalRedirectUrl
        //  contains the redirect URL that would've been taken...
        //  Display the redirect URL, which was not taken...
        printf("Redirect URL:\n");
        printf("%s\n",http.finalRedirectUrl());

    }
    else {
        printf("Not redirected.\n");
    }

    status = http.get_LastStatus();
    if (status == 200) {
        printf("status = 200, OK!\n");
    }
    else {
        printf("HTTP Response status = %d\n"
            ,status);

        //  Display the complete response header.
        printf("%s\n",http.lastResponseHeader());
    }

    }

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.

Mail Component · .NET Mail Component · XML Parser