The effect of the magnetospheric MHD cavity on the excitation of the magnetosphere by stochastic and unsteady hydromagnetic waves incident from the solar wind is investigated theoretically by using a one-dimensional nonuniform model of the medium. It is shown that most of the energy of stochastic waves is reflected from the magnetopause and that the only waves that penetrate into the magnetosphere are those with frequencies in narrow spectral ranges near the eigenfrequencies of the cavity. These waves lead to steadystate excitation of the eigenmodes of the cavity, the energy of which is determined by the spectral density of the energy flux of the incident waves at the corresponding eigenfrequencies. The energy of the eigenmodes penetrates through the opacity barrier in the vicinity of the Alfv,n resonance points (each corresponding to a particular mode), where the perturbation amplitude is sharply amplified, so the total energy localized close to the Alfv,n resonance point is much higher than the total energy of the corresponding eigenmode. In the vicinities, the perturbation energy is dissipated by the finite conductivity of the ionosphere, the dissipation power being equal to the energy flux of the incident waves that penetrates into the magnetosphere. The case of unsteady waves is analyzed by considering a wave pulse as an example. It is shown that most of the energy of the wave pulse is reflected from the magnetopause. The portion of the incident perturbation that penetrates into the magnetosphere leads to unsteady excitation of the eigenmodes of the magnetospheric cavity, which are then slowly damped because part of the energy of the cavity is emitted through the magnetopause back to the solar wind while the other part penetrates into the vicinities of the Alfv,n resonance points. In the vicinities, the perturbation is an Alfv,n wave standing between magnetically conjugate ionospheres and its energy is dissipated by the finite conductivity of the ionosphere at a rate slower than the damping rate of the eigenmodes of the cavity.