In a way, John McCain trained in the art of defiance years before his de-winged aircraft cratered at a Hanoi lake.

His Viet Cong captors pulled him from the water, stabbed him repeatedly with a bayonet and crushed his shoulder with a rifle butt. His right knee was twisted at a 90-degree angle. Provide helpful information, they told him, and medical care for his shattered body would be provided. McCain refused, and he was beaten unconscious.

Prison guards were in charge at that moment, and some semblance of that violent cause and effect — McCain’s refusal to abide, leading to anguishing torture — would be a recurring feature of his more than five years of imprisonment until his release with hundreds of other prisoners in March 1973.

His captors’ guards were in a long line of disciplinarians who tried and failed to corral McCain for decades, stretching from his maverick persona cemented at the Naval Academy to his rebellious streak against the leader of his party, President Donald Trump.

Yet McCain’s infamous streak of bucking authority at Annapolis may have prepared him to defy his captors and survive the physically and psychologically harsh conditions of confinement at Hoa Lo Prison, better known as the Hanoi Hilton.

“Saying no to his captors came easily and naturally to John McCain,” said Alvin Townley, author of “Defiant,” a history of the most die-hard American prisoners of the Vietnam War.

• • •

Before his captivity, McCain honed the art of insubordination at a place created to pulverize individualism into obedience.

“I was really rebellious,” McCain said of his time as a midshipman at the Naval Academy, class of 1958. “I mean, really rebellious.” Years later, he said he resented the academy and the hazing rituals that haunted him and his father before him.

His father, John S. McCain Jr., was an undersized but scrappy “middie” who earned poor marks at the academy before he commanded all forces in Vietnam during his son’s captivity. The family’s naval patriarch, John S. “Slew” McCain Sr., was posthumously awarded the rank of full admiral. He died in 1945, four days after he witnessed the Japanese surrender aboard the USS Missouri.

“Slew” McCain was also a mediocre midshipman who surpassed fellow graduates who’d had much higher grades, as if success following a tumultuous period at Annapolis was hereditary.

John S. McCain III quickly took up that mantle. He developed a reputation for partying and defying authority as he struggled to reconcile a career in the Navy.

“There were times in my youth when I harbored a secret resentment that my life’s course seemed so preordained,” he wrote in his 1999 memoir, “Faith of My Fathers.”

Sarcastic yet iron-willed, McCain wrote in his 1999 book that he scaled the university’s wall (a serious infraction) countless times to drink and flirt with women in town. To be around McCain away from school grounds, one classmate said, “was like being in a train wreck,” biographer Robert Timberg wrote in “John McCain: An American Odyssey.”

Demerits piled up faster than the homework assignments McCain often ignored. McCain tap-danced on the line between perpetual misconduct and expulsion. He successfully graduated in 1958. Only five other midshipmen out of 899 ranked lower.

• • •

Nine years later, McCain would finally be ahead of his cohorts — no American prisoner arrived at the Hanoi Hilton in worse condition than he did, John Hubbell wrote in the book “P.O.W.”

He endured years of beatings and torture. Survival and defiance, and the link between them, seemed intuitive for McCain. In spring 1968 he yelled to visiting North Vietnamese dignitaries that he would reject early release and amnesty. It earned McCain solitary confinement for two years.

“You can’t imagine the example John set for the rest of the camp by doing that,” former prisoner Jack Van Loan told Timberg.

In 1973, in a moment that marked the end of the Vietnam War for many, 591 prisoners were released as part of Operation Homecoming. Despite his captors’ repeated offers of early release, McCain had stayed true to the prisoner edict: that to return with honor, you must return together.

It was perhaps one of few moments when John S. McCain III followed the rules.