WASHINGTON — Defense Secretary Robert M. Gates and the nation’s top military officer said on Thursday that it was critical for the United States to maintain ties with Pakistan despite growing anti-Americanism in the Pakistani military and the worst relationship between the two countries in years.