U.S. Secretary of Defense Leon Panetta receives a plaque after he addressed cadets at the PLA Engineering Academy of Armored Forces in Beijing on Sept. 19, 2012. (LARRY DOWNING/REUTERS)

The United States and China have alternately embraced and poked each other in the eye during a three-day visit by Defense Secretary Leon E. Panetta that reflects the two superpowers’ growing, and sometimes conflicting, strategic ambitions.

On Wednesday, Panetta met with Xi Jinping, the Chinese vice president who is expected to lead the country for the next decade. Xi had mysteriously vanished from view for much of September and canceled a meeting with Secretary of State Hillary Rodham Clinton.

Xi and Panetta shook hands vigorously for the cameras and proclaimed their commitment to a closer relationship between Beijing and Washington.

That scripted encounter, however, followed some chillier examples of the mistrust the United States and China still harbor about each other’s intentions.

On Tuesday, protesters surrounded and jostled a car carrying U.S. Ambassador to China Gary Locke and briefly prevented him from entering the embassy. Locke said he never felt in danger and reported that Chinese police cleared the scene quickly. U.S. officials nevertheless formally complained to China’s Foreign Ministry, urging the government “to do everything they can to protect our personnel in China,” Locke told reporters Wednesday.

Public demonstrations rarely occur in China without official endorsement or backing. The protesters who accosted Locke had been raising a ruckus for days at the Japanese Embassy nearby, angry about a territorial dispute with Tokyo over some uninhabited islands in the East China Sea.

During his visit to Asia, Panetta has urged both sides to resolve that conflict peacefully, reiterating that the United States does not take sides in the matter of control of the islands, known as Senkaku in Japan and Diaoyu in China.

But he ruffled feathers in Beijing when, a day before his arrival, he announced a deal with Tokyo to install a powerful radar tracking station in southern Japan. Panetta insisted the anti-missile radar was solely intended to monitor North Korea and its growing nuclear arsenal, but suspicions persist in China that it is also a target of the radar.

Panetta’s visit to China is his first since he became defense secretary in July 2011 and offers an opportunity to explain the Obama administration’s new strategic “pivot” to Asia. Chinese leaders have repeatedly asked Panetta why the Pentagon is planning to send more warships, surveillance planes and other military assets to the region.

In a speech Wednesday to cadets at a People’s Liberation Army engineering academy, Panetta responded that “our rebalance to the Asia-Pacific region is not an attempt to contain China.” Rather, he said, “it is about renewing and revitalizing our role in a part of the world that is rapidly becoming more critical to our economic, diplomatic and security interests.”

The cadets applauded politely but also posed some tough questions. One noted that Washington routinely sells arms to Taiwan, even though U.S. officials know that such sales irritate China. “Do you think that is contradictory to what you just said?” the cadet asked, referring to Panetta’s insistence that the United States wants a closer relationship with China.

U.S. officials said Panetta fielded similar questions during his meetings with military officials and with Xi, who is expected to ascend to the country’s presidency this fall.

Panetta said neither Xi nor his subordinates offered any explanation for why he had disappeared from public view for two weeks, canceling meetings with Clinton and other foreign dignitaries. Xi’s absence had triggered a tsunami of gossip, including unsubstantiated rumors that he had suffered a heart attack or been targeted for assassination.

Xi reappeared briefly Saturday, making an unannounced tour of a university campus. But his meeting with Panetta was the first time since Sept. 1 that he showed up for a scheduled public event with journalists present.

As the cameras rolled, he energetically shook Panetta’s hand and exchanged pleasantries. The two leaders then met in private for an hour and 15 minutes, nearly 30 minutes longer than scheduled.

“My impression was that he was very healthy and very engaged,” Panetta told reporters afterward. “He’s someone who speaks frankly, who speaks candidly. You don’t get the sense he’s pulling his punches or reading talking points, but that he’s speaking from the heart.”