WASHINGTON — The two major presidential rivals sharpened their long-standing dispute over the Iraq war on Tuesday, Democratic Sen. Barack Obama calling it a costly distraction that must end, while Republican Sen. John McCain insisted it is a conflict the United States has to win.

"Iraq is not going to be a perfect place, and we don't have unlimited resources to try and make it one," Obama said in a speech in which he also said the United States must shift its focus to defeating the Taliban and al-Qaida in Afghanistan.

Rebutting swiftly, McCain said Obama "will tell you we can't win in Afghanistan without losing in Iraq. In fact, he has it exactly backwards."

While the two men agreed on the importance of prevailing in Afghanistan, the dispute veered in a new direction when it came to the tribal areas of next-door Pakistan, where terrorist Osama bin Laden and his men are thought to be hiding.

McCain accused Obama of "trying to sound tough" by speaking publicly of taking unilateral action against those blamed for the terror attacks of Sept. 11, 2001.

Undeterred, Obama said, "If Pakistan cannot or will not act, we will take out high-level terrorist targets like bin Laden if we have them in our sights."

Despite his rhetoric, Obama refrained from saying the administration's so-called surge in troop strength in Iraq had failed. Aides said his campaign Web site had been altered in recent days to remove references to that effect.

The two men sparred as Obama looked ahead to an overseas trip that will include stops in Iraq and Afghanistan, two countries where thousands of U.S. combat forces are engaged in combat as part of the global war on terror. Given differences in age and experience between the two rivals, Obama's trip has taken on elements of an audition for a man seeking overall charge of U.S. war policy as well as foreign policy in general.

A Washington Post poll released before the two men spoke showed the country evenly divided on whether Obama, 46, now serving his first term in the Senate, would be a good commander in chief. The 71-year-old McCain, who was a Vietnam prisoner of war and has long Senate experience with defense issues, was widely viewed favorably on the same question.

An AP-Yahoo poll taken last month showed 39 percent of those surveyed said McCain would do a better job of handling Iraq, compared with 33 percent for Obama.

In his remarks, Obama pushed back against his rival's recent comments that the Bush administration's 18-month increase in troop strength in Iraq has been a success that warrants a change in position on the war.

"This argument misconstrues what is necessary to succeed in Iraq and stubbornly ignores the facts of the broader strategic picture that we face," the Illinois senator said.

"In the 18 months since the surge began, the strain on our military has increased, our troops and their families have borne an enormous burden, and American taxpayers have spent another $200 billion in Iraq," he said.

In Afghanistan, "June was our highest casualty month of the war. The Taliban has been on the offensive, even launching a brazen attack on one of our bases. Al-Qaida has a growing sanctuary in Pakistan," Obama added.

In reply, McCain belittled his younger rival, who he said was speaking about the war before traveling overseas and talking to Gen. David Petraeus, the overall commander on the ground.

"In my experience, fact-finding missions usually work best the other way around: First you assess the facts on the ground, then you present a new strategy," McCain said.

Obama said he stands by his longtime proposal to withdraw U.S. combat troops. "We can safely redeploy ... at a pace that would remove them in 16 months" from the time he takes office, he said. "We will make tactical adjustments as we implement this strategy — that is what any commander in chief must do," he said.

Later, in an interview with PBS, Obama said one of the issues he wants to discuss with Petraeus and others while in Iraq concerns the resources they will seek to carry out a post-combat mission of protecting U.S. personnel and bases, training Iraqi forces and conducting counterinsurgency attacks against al-Qaida.

Obama's aides billed his speech as a major address, and in it the Illinois senator sketched a foreign policy for a new administration.

Apart from ending the war in Iraq, he cited finishing the fight against al-Qaida and the Taliban, securing all loose nuclear weapons and materials, achieving energy security and rebuilding international alliances.

Yet, he stressed that ending the war in Iraq was essential.

"This war distracts us from every threat we face and so many opportunities we could seize," he said.

Obama salted his speech with criticism of McCain, who frequently cites his Senate experience on matters of defense and foreign policy.

"I opposed going to war in Iraq. Sen. McCain was one of Washington's biggest supporters of the war," he said.

"I warned that the invasion ... would fan the flames of extremism and distract us from the fight against al-Qaida and the Taliban. Sen. McCain claimed that we would be greeted as liberators."

McCain's campaign theme for the day was the economy, but he decided that Obama's remarks were too important to go without a rebuttal.

Speaking in New Mexico, he said Obama's assessment of the increase is troop strength was wrong. "The surge has succeeded. And because of its success, the next president will inherit a situation in Iraq in which America's enemies are on the run and our soldiers are beginning to come home."