This site has been acquired by Toptal.com.
(Attention! API endpoint has changed)

HTML Minifier

Online HTML Minifier/Compressor. Free! Provides an API. Simple Quick and Fast.

C# Async Example

Thanks to Dennis Janiak for contributing this example.

public static class HtmlMinifier
{
    private const string URL_HTML_MINIFIER      = "https://www.toptal.com/developers/html-minifier/raw";
    private const string POST_PAREMETER_NAME    = "input";

    public static async Task<String> MinifyHtml(string inputHtml)
    {
        List<KeyValuePair<String, String>> contentData = new List<KeyValuePair<String, String>>
        {
            new KeyValuePair<String, String>(POST_PAREMETER_NAME, inputHtml)
        };

        using (HttpClient httpClient = new HttpClient())
        {
            using (FormUrlEncodedContent content = new FormUrlEncodedContent(contentData))
            {
                using (HttpResponseMessage response = await httpClient.PostAsync(URL_HTML_MINIFIER, content))
                {
                    response.EnsureSuccessStatusCode();
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }
    }
}

Usage:

using (Task<String> task = HtmlMinifier.MinifyHtml("/* test commentary */  ... 
   "))
{
    task.Wait();
    Console.WriteLine(task.Result); // ...
}

C# Example

Thanks to Juan Herrera for contributing this example.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.toptal.com/developers/html-minifier/raw");
request.Method = "POST";
string formContent = "input=" + htmlContent.Text;
byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream str = request.GetRequestStream();
str.Write(byteArray, 0, byteArray.Length);
str.Close();

WebResponse response = request.GetResponse();
str = response.GetResponseStream();
if (str != null)
{
  StreamReader reader = new StreamReader(str);
  htmlMinified.Text = reader.ReadToEnd();
  reader.Close();
  str.Close();
}
response.Close();

Languages

Click on the language of your choice to see an example: