Afghan President Hamid Karzai lashed out at the United States in strikingly acerbic terms Sunday, implying that the American military was stoking violence in collusion with the Taliban to justify a prolonged presence here and charging that foreign troops were harassing Afghan university students.

The remarks painted an embarrassing picture of discord that marred a visit by Defense Secretary Chuck Hagel, his first foreign trip as Pentagon chief, and plunged the tenuous allies into crisis mode at a time when the United States is struggling to wind down the unpopular war.

Afghan and American officials provided differing explanations for the cancellation of a joint news conference scheduled for Sunday night that had been expected to be the centerpiece of Hagel’s trip. U.S. officials said they scrapped the event in consultation with the Afghan government because of an unspecified security threat. Karzai’s spokesman, Aimal Faizi, rejected the notion that the palace would have been a dangerous place to hold a news conference.

“From our side, we saw no threat,” he said.

At best, the move left the impression that after more than a decade of war and billions of U.S. dollars spent, the United States deemed the risk of holding a news conference in the most barricaded quarter of the capital as unreasonably high. In stark contrast to Kabul visits by other U.S. defense secretaries, the trip did not include upbeat public pronouncements by Hagel about the state of the U.S. mission.

Hagel and Karzai did meet privately Sunday night. After the meeting, the secretary told reporters he was hopeful the two countries could overcome the latest crisis. “I know these are difficult issues for President Karzai and the Afghan people,” Hagel said.

In a televised speech about violence against women, Karzai said two fatal bombings carried out Saturday, including one outside the Defense Ministry, should not be interpreted as a Taliban show of force aimed at undermining the U.S. military, which is considering keeping a small force in Afghanistan after its wartime mandate expires at the end of 2014.

“In reality, the bombs that went off yesterday under the name of the Taliban were a service to the foreigners,” Karzai said, casting doubt on the assertion of responsibility made by the Taliban, which said that the attacks were carried out to mar Hagel’s visit. Karzai said the blasts helped Americans justify a prolonged troop presence in Afghanistan. “We have been down this road before too many times,” he added.

U.S. officials struggled to make sense of the tone and timing of Karzai’s remarks, but the top U.S. commander in Afghanistan dismissed the idea that the U.S. military could be complicit in attacks.

“We have fought too hard over the past 12 years,” Gen. Joseph F. Dunford Jr. told reporters in Kabul on Sunday. “We have shed too much blood over the past 12 years, we have done too much to help the Afghan security forces grow over the past 12 years to think that violence or instability would be to our advantage.”

Dunford suggested Karzai’s anti-American rhetoric could be political gamesmanship.

“He’s a head of state that has both an internal and external audience,” Dunford said. “He knows far better than I do how to manage internal and external audiences.”

A senior Afghan official recently said that Karzai has opted to take the Americans to task in public because he feels Western officials don’t take him seriously and fail to listen to him during private meetings. The official spoke on the condition of anonymity to talk candidly about Karzai’s thinking.

But Faizi, the president’s spokesman, said Karzai “raises his voice when there are concerns about issues that are not moving forward.”

Hours after the speech, Karzai’s office issued an executive order threatening to take “legal action” if international forces don’t stop harassing, annoying and detaining Afghan students.

The statement said Karzai “considers such acts a serious breach of Afghan sovereignty that stand in utter contradiction to the country’s enforced laws.”

Dunford, who was informed by journalists about the executive order, assured reporters that the international coalition was not harassing Afghan college students.

Karzai’s anger has been building in recent weeks. Two weeks ago, he imposed a two-week deadline for the expulsion of U.S. Special Operations forces from Wardak, a crucial eastern province, accusing the forces of murder and other abuses. The U.S. military said an investigation into the claims turned up no evidence to substantiate them and disregarded the ultimatum, saying U.S. forces would leave the area when it is prudent to do so.

Last week, Karzai balked at the terms of a plan for the Afghan government to assume responsibility for the American detention center here, hastily canceling a handover ceremony that had been scheduled to take place Saturday. U.S. commanders say they will not release prisoners who could put their troops in harm’s way.

Pentagon spokesman George Little denied that Sunday’s news conference had been canceled in response to Karzai’s remarks. He cited security concerns, which he declined to elaborate on, as the reason.

Dunford said Karzai’s heated rhetoric should not create the impression that trust between the two nations is irreparably broken. In fact, he argued, senior U.S. and Afghan security officials see eye to eye on most things.

“There is not a glimmer of daylight in our perspectives,” he said.

A U.S. official, who spoke on the condition of anonymity to discuss Washington’s perspective on Karzai’s latest outburst, said the U.S. government still believes it has a fundamentally constructive relationship with the Afghan government, including its president.

“We have told him in private that public criticism is unhelpful, especially when there is no basis in fact for some of the claims he makes,” the official said.

Hagel, who represented Nebraska in the U.S. Senate from 1997 to 2009, said he recognized the difficult position Karzai is in. “I was a politician once, so I can understand the kind of pressures” he faces, Hagel said. He added that he hoped the allies “can move forward, and I have confidence that we will.”