Mike Brown celebrated his England return with the winning try as Harlequins took a giant step towards securing a place in the quarter finals of the Heineken Cup.
The full-back took advantage of Danny Care's chip, and a mistake from his new England colleague Charlie Sharples, to touch down in the 73rd minute. Gloucester had proved a tough nut to crack up until that point, with the Cherry and Whites cancelling out Matt Hopper's opening try with an effort from James Simpson-Daniel.
Harlequins trailed 11-10 at the interval and they were 14-13 down when Chris Robshaw, one of the leading candidates for the England captaincy, three times declined to take the points as Gloucester were penalised at successive five-metre scrums.
But Harlequins conjured up one last opportunity which Brown snapped up, and a victory in Connacht next week should be enough to secure a quarter-final place, even if Toulouse top the group.
Harlequins started with three of three four players who were called into the England squad this week, with Robshaw and Brown joined by centre Jordan Turner-Hall while Joe Marler was on the bench. They were also boosted by the return to action of Nick Evans, although the influential New Zealand fly-half book-ended the first half with relatively simple missed penalties.
That allowed Gloucester to take a narrow 11-10 lead into the interval, having quelled a bright start from Harlequins which saw them surge ahead with an expertly-taken try from Hopper.
Harlequins had already sounded warning of their determination to run from deep and attack at pace when Brown, Hopper and Turner-Hall all passed out of the tackle to send Mauri Fa'asavalu charging into the Gloucester 22. Hopper scorched up on the outside but Fa'asavalu's pass was too low, even for the diminutive former Cornish Pirates centre, and he knocked it on.
Freddie Burns and Evans exchanged penalties before Harlequins punished the Gloucester fly-half for sending a clearance directly into touch with an exhilarating opening try from Hopper. Harlequins won clean lineout ball, wing Tom Williams off-loaded out of the tackle, Hopper's pass was flicked on expertly by Evans and Brown released Monye down the left.
Although Monye was halted, Gloucester could not control the ball. Brown scrambled forward and when the ball came to back Hopper, he stepped around the stranded Scott Lawson to score under the posts. Evans converted but Harlequins failed to hammer home their ascendancy and Gloucester worked themselves into the lead with a Burns penalty and Simpson-Daniel's clinical try.
Burns had missed with one shot immediately after Hopper's try but he succeeded from virtually in front of the posts after the Harlequins scrum had been penalised.
Burns was having a mixed day with the boot but his grubber kicked forced Harlequins back into their own 22 and Joe Gray's crooked lineout throw gifted Gloucester a prime attacking platform. And they needed no second invitation, executing a training-ground move which finished with Simpson-Daniel running an arcing line through Hopper's attempted tackle to score.
Evans failed with a simple penalty attempt, after Gloucester had crept offside, to give Gloucester the half-time advantage and he missed again early in the second half. The lead switched hands again as Evans rediscovered his range but Gloucester's scrum were on top and earned another penalty for Burns, which he converted.
Mike Tindall and Burns combined to send Jonny May away down the left but Harlequins scrambled well and then turned the tables, with Brown making ground in heavy traffic. Olly Kohn picked up and galloped on before feeding Care, who chipped ahead. May gathered but was tackled behind his line to concede the five metre scrum.
The Harlequins pack, now with Marler installed on the loosehead following his 50th-minute introduction, won three successive penalties before Gloucester prop Nick Wood was sin-binned. On each occasion, Harlequins opted for a re-set instead of taking the points to get ahead. And on after the seventh set scrum, Care was penalised for being offside and Gloucester cleared their lines.
But the respite was only temporary. Monye's chip and chase forced May to run the ball from behind his own posts and the Gloucester full-back was turned over. Harlequins moved the ball quickly. Care stabbed the kick through and when Sharples failed to deal with it, Brown was on hand to touch down.
Evans converted from wide on the left and Harlequins sealed the victory with some stoic defence as Gloucester's last chance disappeared with a Simpson-Daniel knock-on.