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)
{
    try
    {
        HttpWebRequest request = WebRequest.Create(strSrc) as HttpWebRequest;

        if(request==null)
            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;

        if(response==null)
            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);

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

        if(b)
            ParseContentDisposition(m_szContentDisposition);

        response.Close();
    }
    catch (System.Net.WebException e)
    {
        if(e.Response!=null)
	        strDest = e.Response.ResponseUri.ToString();
        return false;
    }

    return true;
}


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

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

    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)
    {
        if(strContentDisposition[i]==';')
	        break;
        else
	        strValue += strContentDisposition[i];
    }

    if(String.IsNullOrEmpty(strValue))
    {
        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

References

Mail Content Disposition Values and Parameters

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read