Buy a spray can of electrical contact cleaner, and hose the dimmer switch down with it, then operate the switch repeatedly, lights on or off doesn't matter. Repeat as necessary until things either work right or the switch self-destructs. Something as essential to safety as the headlights deserves a new dimmer switch- - - -sort of cheap life insurance if you plan to drive at night.
Jerry