The House on Wednesday rejected a bipartisan effort to force President Obama to withdraw U.S. forces from Iraq and Syria by the end of the year.

The measure was defeated, 288-139. It would have directed that troops be withdrawn within 30 days of passage, or by the end of the year if Obama determines an immediate withdrawal is not safe.

Rep. Jim McGovern, D-Mass., said the resolution was needed to "force Congress to do its job" and vote on whether to formally authorize military action against Islamic State militants. Obama requested such a resolution in February, but it has stalled in Congress.

"If we are going to invest a generation or more of our blood and our treasure in this war, and if we're going to continue to tell our armed forces that we expect them to fight and die in these wars, it seems to me the least we can do is stand up and vote to authorize these wars -- or we should end them," McGovern said in a fiery speech on the House floor.

McGovern, who co-sponsored the resolution with Reps. Walter Jones, R-N.C., and Barbara Lee, D-Calif., said lawmakers owe it to the American people to vote on a war resolution.

"We owe that to our troops and their families, and we owe that to the oath of office each of us took to uphold the Constitution of the United States," McGovern said.

Opponents called the measure dangerous, saying it could lead the U.S. to "walk away" from the region, leaving it more unsettled and increasing the chances of the Islamic State becoming even stronger.

Rep. Ed Royce, R-Calif., chairman of the House Foreign Affairs Committee, said approval of the resolution would be "seen as a no-confidence vote" in U.S. troops in Iraq and Syria. Withdrawal of U.S. troops could lead to mass killings, assaults and the forced sex trade of teenage girls, Royce said.

Rep. Eliot Engel of New York, the senior Democrat on the foreign affairs panel, said he agreed with McGovern that Congress should vote on a war powers resolution. But he said immediate withdrawal of U.S. troops was "no way to defeat ISIS," using an alternate acronym for the militant group.

"It's a difficult fight, but I don't think we should walk away," Engel said.

Gen. Martin Dempsey, chairman of the Joint Chiefs of Staff, said withdrawal of U.S. troops "would be a mistake for obvious reasons."

The U.S. has national security interests within Iraq and also has interest in maintaining credible safe and reliable allies in the region, Dempsey told the House Armed Services Committee on Wednesday.

"Our withdrawal would challenge (that) and put us at greater risk over time," Dempsey said.