The Spurs knew what they were getting into when they arrived at the Toyota Center on Sunday night.

In the Houston Rockets, they were getting a cornered opponent scrapping for a spot at the bottom of the Western Conference playoff bracket.

In Rockets guard James Harden, they were getting a singular foe who has made a living, not to mention tens of millions of dollars, with his ability to get to the free-throw line.

That foreknowledge would be of little comfort a few hours later, when the Spurs left Houston saddled with a 96-95 loss made possible in part by Harden’s 17 visits to the foul stripe.

“Give him a lot of credit, he played well tonight,” Spurs forward Tim Duncan said. “I don’t think he earned 17 free throws. I think we had a couple in there that were missed calls.”

Harden finished with 29 points, 15 of which came from the foul line.

He capped his night by hitting the game-winning jumper, an off-balance 15-footer with 4.5 seconds left, gaining the necessary space perhaps because his defender, Kawhi Leonard, was reticent about giving him free-throw tries No. 18 and 19.

By night’s end, Harden had gone to the charity stripe more times than any Spurs opponent this season. The previous high was 11, shared by the Oklahoma City duo of Kevin Durant and Russell Westbrook and Indiana’s David West.

“He was aggressive with it,” Duncan said. “He took shots and made (officials) make calls and kept them right in the game.”

Combined with Oklahoma City’s victory over Portland, the loss cut the Spurs’ lead atop the Western Conference to 1 1/2 games with a brutal homestand (Denver, the L.A. Clippers and Miami) looming this week.

As such, nights like Sunday are particularly tough for the Spurs (53-17) to swallow.

It was a Spurs-Rockets game Jeff Van Gundy and the old Gregg Popovich would have loved, a low-scoring, grind-it-out affair that came down to the final possession.

“It was a hard-fought game,” said Manu Ginobili, who was 1 of 6 for three points and missed a putback at the buzzer. “It felt like a playoff one.”