Tip: HTTP Content Disposition

When a programmer is writing an HTTP download engine, sometimes the engine will encounter URLs that don't contain a file name. By 'URLs', I mean the source URL (original URL), and the response URL(also known as the redirected URL) that might — or might not — be the same as the source URL. In other words, the response URL is the URL that responds to your HTTP request.

If you encounter a URL that does not contain the file name, then you can resort to finding the file name in content disposition header. However, because the content disposition header is not part of the HTTP 1.1 standard, not all web sites implement it; however, it is widely implemented in most web sites.

Unfortunately, .NET 2.0 HttpWebResponse class does not support querying the file name from this header directly. You can still parse this information yourself. Here are the steps to do so, using the .NET 2.0 HttpWebResponse class:

  • Use WebRequest's static Create method to pass the source URL as the only parameter and the Create method will return a HttpWebRequest object.
  • Pass the web proxy from WebRequest's GetSystemWebProxy method or WebRequest's DefaultWebProxy property to HttpWebRequest object's Proxy property. This step is optional. However, if you do not do it, those people who can only access internet through a web proxy, cannot access the web.
  • Use the HttpWebRequest object's GetResponse method to get a valid HttpWebResponse object
  • Query if the "content-disposition" string exists in the HttpWebResponse object's Header property.
  • If "content-disposition" exists, since we are only interested in the file name, search for the "filename" among the keyvalue pairs.
  • Each keyvalue pair is separated by a semicolon and the key is separated from the value with an assignment character. Example, filename="Hello.jpg".
  • Last step would be to trim away the double quotes around the file name, if there is any.
public bool GetResponseUrl(
    System.String strSrc, 
    ref System.String strDest)
        HttpWebRequest request = WebRequest.Create(strSrc) as HttpWebRequest;

            return false;

        System.Net.IWebProxy iwpxy = WebRequest.GetSystemWebProxy();
        System.Uri url = new Uri("http://www.example.com");
        System.Uri urlProxy = iwpxy.GetProxy(url);
        WebProxy wpxy = new WebProxy();
        if (url != urlProxy)
            wpxy.Credentials = iwpxy.Credentials;
            wpxy.Address = urlProxy;

        request.Proxy = wpxy;  

        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            return false;

        strDest = response.ResponseUri.ToString();

        for ( int i = 0; i < response.Headers.Count; ++i )
	        System.Diagnostics.Debug.Print("{0} : {1}", response.Headers.Keys[i], response.Headers[i]);
	        m_dictHeaders[response.Headers.Keys[i]] = response.Headers[i];

        bool b = m_dictHeaders.TryGetValue("content-disposition", out m_szContentDisposition);

            b = m_dictHeaders.TryGetValue("Content-Disposition", out m_szContentDisposition);


    catch (System.Net.WebException e)
	        strDest = e.Response.ResponseUri.ToString();
        return false;

    return true;

public bool ParseContentDisposition(System.String str)
        return false;

    System.Diagnostics.Debug.Print("Content Disposition");

    String strFilenameKey = "filename";
    String strFilenameValue = String.Empty;

    int pos = 0;
    m_szCDFilename = System.String.Empty;
    return ExtractContentDisposition(pos, str, strFilenameKey + "=", ref m_szCDFilename);

public bool ExtractContentDisposition(
    int pos,
    System.String strContentDisposition, 
    System.String strKey, 
    ref System.String strValue )
    System.String strContentDisposition2 = strContentDisposition.ToLower();
    pos = strContentDisposition2.IndexOf(strKey, pos);

    for(int i=pos+strKey.Length; i<strContentDisposition.Length; ++i)
	        strValue += strContentDisposition[i];

        return false;

    String strDelimit="\"\' \t";
    strValue = strValue.Trim(strDelimit.ToCharArray());

    return true;

Note: If the file name is in the URL ( that is, it is at the right end of the URL), then most likely there won't be a content disposition for you to get the file name. So the 'CD Filename' would be empty


Mail Content Disposition Values and Parameters

This article was originally published on August 17th, 2009

About the Author

Wong Shao Voon

I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me.

When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day.

I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts.

I also writes articles for CodeGuru; I have a few ideas to write about but never get around writing because of hectic schedule.


Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date