Good advice. Smile, don't fight, do not argue.
The best advice would be to prepare yourself as best as you can for the possibility of such negative encounters. Study what you can, must, should, and should NOT say, and then try to keep your brain engaged and stick to those concepts.
Clamming up completely isn't the very best choice (unless it is the best choice FOR YOU due to your personality and knowledge) but running on at the mouth -- either with hostility or acquiescence -- is much worse.
The classic responses, "Am I under arrest?" "I don't consent to any searches," and even "I would love to help you officer, but I think I should speak to my lawyer before making any further statements," and so forth, repeated firmly and calmly should serve to put the officer on notice that you probably won't make a convenient mistake giving him cause to arrest you, make an extensive search, and/or otherwise hassle you further.
It just might also plant a seed in his mind that you're probably knowledgeable about your rights enough to give him trouble later with IA and his Chief if he really steps over the line.
It is terribly embarrassing, frustrating, and enraging to be treated unfairly and with such hostility by any person of authority, but take heart in the fact that we live in a place where such abuses are about as limited as they can be, are heavily officially investigated and reported upon, and the worst cop out there isn't going to KILL you just 'cause he doesn't like you. (There are parts of the world where that might not be so...)
Keep positive, focus on being the calm one -- the "bigger" man. Comply with everything you MUST comply with, minimize the damage you do yourself, and live to fight the battle more effectively, later, through official channels.