WASHINGTON — President Trump vetoed a bipartisan resolution on Tuesday that would have forced an end to American military involvement in Saudi Arabia’s civil war in Yemen, rejecting an appeal by lawmakers to his own deeply rooted instincts to withdraw the United States from bloody foreign conflicts.

The veto, only the second time Mr. Trump has used his power to block legislation passed by both houses of Congress, strikes down a resolution that invoked the War Powers Act to distance the United States from a four-year conflict that has killed thousands of civilians and resulted in a widespread famine.

The measure was a rebuke of Mr. Trump’s support for Saudi Arabia even after the killing of the dissident journalist Jamal Khashoggi. It was opposed by several of the president’s top advisers, including Secretary of State Mike Pompeo and the national security adviser, John R. Bolton, according to people who spoke with White House officials.

“This resolution is an unnecessary, dangerous attempt to weaken my constitutional authorities, endangering the lives of American citizens and brave service members, both today and in the future,” Mr. Trump said in his veto message.