C# Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

C# Examples

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


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

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

 

 

 

 

 

 

HTTP Redirect Handling

Examine HTTP redirects.

Download Chilkat .NET for 2.0 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

Chilkat.Http http = new Chilkat.Http();

bool success;

//  Any string unlocks the component for the 1st 30-days.
success = http.UnlockComponent("Anything for 30-day trial");
if (success != true) {
    MessageBox.Show(http.LastErrorText);
    return;
}

string url;
string 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.FollowRedirects = true;

//  Send the HTTP GET and return the content in a string.
html = http.QuickGetStr(url);
if (html == null ) {
    MessageBox.Show(http.LastErrorText);
}

//  On success, LastErrorText will provide information about
//  what happened during the call.
textBox1.Text += "--------------- LastErrorText ------------------" + "\r\n";
textBox1.Refresh();
textBox1.Text += http.LastErrorText + "\r\n";
textBox1.Refresh();
textBox1.Text += "------------------------------------------------" + "\r\n";
textBox1.Refresh();

//  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.WasRedirected == true) {
    textBox1.Text += "Chilkat HTTP followed the redirect." + "\r\n";
    textBox1.Refresh();

    //  Display the final redirect URL:
    textBox1.Text += "Final URL:" + "\r\n";
    textBox1.Refresh();
    textBox1.Text += http.FinalRedirectUrl + "\r\n";
    textBox1.Refresh();

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

}
else {
    textBox1.Text += "Not redirected." + "\r\n";
    textBox1.Refresh();
}

status = http.LastStatus;
if (status == 200) {
    textBox1.Text += "status = 200, OK!" + "\r\n";
    textBox1.Refresh();
}
else {
    textBox1.Text += "HTTP Response status = "
         + Convert.ToString(status) + "\r\n";
    textBox1.Refresh();

    //  Display the complete response header.
    textBox1.Text += http.LastResponseHeader + "\r\n";
    textBox1.Refresh();
}

//  Now try it without following redirects:
textBox1.Text += "-------- Now trying without following redirects...." + "\r\n";
textBox1.Refresh();
http.FollowRedirects = false;

//  Send the HTTP GET and return the content in a string.
html = http.QuickGetStr(url);
if (html == null ) {
    //  the HTML string can NULL if a 302 redirect response is received.
    textBox1.Text += "HTML string returned NULL..." + "\r\n";
    textBox1.Refresh();
}

//  On success, LastErrorText will provide information about
//  what happened during the call.
textBox1.Text += "--------------- LastErrorText ------------------" + "\r\n";
textBox1.Refresh();
textBox1.Text += http.LastErrorText + "\r\n";
textBox1.Refresh();
textBox1.Text += "------------------------------------------------" + "\r\n";
textBox1.Refresh();

//  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.WasRedirected == true) {
    textBox1.Text += "This was a redirect response" + "\r\n";
    textBox1.Refresh();

    //  When redirects are not followed, FinalRedirectUrl
    //  contains the redirect URL that would've been taken...
    //  Display the redirect URL, which was not taken...
    textBox1.Text += "Redirect URL:" + "\r\n";
    textBox1.Refresh();
    textBox1.Text += http.FinalRedirectUrl + "\r\n";
    textBox1.Refresh();

}
else {
    textBox1.Text += "Not redirected." + "\r\n";
    textBox1.Refresh();
}

status = http.LastStatus;
if (status == 200) {
    textBox1.Text += "status = 200, OK!" + "\r\n";
    textBox1.Refresh();
}
else {
    textBox1.Text += "HTTP Response status = "
         + Convert.ToString(status) + "\r\n";
    textBox1.Refresh();

    //  Display the complete response header.
    textBox1.Text += http.LastResponseHeader + "\r\n";
    textBox1.Refresh();
}

 

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

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

Email Component · XML Parser