Solving $\int_{-\infty}^{\infty}\frac{x^2e^x}{(1+e^x)^2}dx$

$\newcommand{\+}{^{\dagger}}% \newcommand{\angles}[1]{\left\langle #1 \right\rangle}% \newcommand{\braces}[1]{\left\lbrace #1 \right\rbrace}% \newcommand{\bracks}[1]{\left\lbrack #1 \right\rbrack}% \newcommand{\dd}{{\rm d}}% \newcommand{\isdiv}{\,\left.\right\vert\,}% \newcommand{\ds}[1]{\displaystyle{#1}}% \newcommand{\equalby}[1]{{#1 \atop {= \atop \vphantom{\huge A}}}}% \newcommand{\expo}[1]{\,{\rm e}^{#1}\,}% \newcommand{\floor}[1]{\,\left\lfloor #1 \right\rfloor\,}% \newcommand{\ic}{{\rm i}}% \newcommand{\imp}{\Longrightarrow}% \newcommand{\ket}[1]{\left\vert #1\right\rangle}% \newcommand{\pars}[1]{\left( #1 \right)}% \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}} \newcommand{\pp}{{\cal P}}% \newcommand{\root}[2][]{\,\sqrt[#1]{\,#2\,}\,}% \newcommand{\sech}{\,{\rm sech}}% \newcommand{\sgn}{\,{\rm sgn}}% \newcommand{\totald}[3][]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}} \newcommand{\ul}[1]{\underline{#1}}% \newcommand{\verts}[1]{\left\vert #1 \right\vert}% \newcommand{\yy}{\Longleftrightarrow}$ This integral is an usual one in Statistical Mechanics $\pars{~\mbox{the integrand is an}\ {\large\tt\mbox{even}}\ \mbox{function}~}$: \begin{align}\color{#0000ff}{\large% \int_{-\infty}^{\infty}\!\!{x^{2}\expo{x}\,\dd x \over \pars{\expo{x} + 1}^{2}}} &= -2\int_{0}^{\infty}x^{2}\,\totald{}{x}\pars{1 \over \expo{x} + 1}\,\dd x = 4\int_{0}^{\infty}{x \over \expo{x} + 1}\,\dd x \\[3mm]&= 4\int_{0}^{\infty}x\pars{{1 \over \expo{x} + 1} - {1 \over \expo{x} - 1}}\,\dd x + 4\int_{0}^{\infty}{x \over \expo{x} - 1}\,\dd x \\[3mm]&= -8\int_{0}^{\infty}{x \over \expo{2x} - 1}\,\dd x + 4\int_{0}^{\infty}{x \over \expo{x} - 1}\,\dd x \\[3mm]&= -2\int_{0}^{\infty}{x \over \expo{x} - 1}\,\dd x + 4\int_{0}^{\infty}{x \over \expo{x} - 1}\,\dd x = 2\int_{0}^{\infty}x\expo{-x}\,{1 \over 1 - \expo{-x}}\,\dd x \\[3mm]&= 2\int_{0}^{\infty}x\expo{-x}\sum_{\ell = 0}^{\infty}\expo{-\ell x}\,\dd x = 2\sum_{\ell = 0}^{\infty}\int_{0}^{\infty}x\expo{-\pars{\ell + 1}x}\,\dd x \\[3mm]&= 2\sum_{\ell = 0}^{\infty}{1 \over \pars{\ell + 1}^{2}}\ \overbrace{\int_{0}^{\infty}x\expo{-x}\,\dd x}^{=\ 1} = 2\ \overbrace{\sum_{\ell = 1}^{\infty}{1 \over \ell^{2}}}^{\ds{=\,\pi^{2}/6}} = \color{#0000ff}{\large{\pi^{2} \over 3}} \end{align}


Expanding on my comment, your problem is that you're encompassing infinitely many poles. Pick a curve that encompasses finitely many, instead. In particular, try integrating over the rectangle with vertices $(-R,0),(R,0),(-R,2\pi i), (R,2\pi i)$. Be a little careful over the top part of the rectangle. For $f(z) = \frac{z^2 e^z}{(1+e^z)^2}$ you have $$f(z+2\pi i) = \frac{(z+2\pi i)^2 e^z}{(1+e^z)^2} = f(z) -4\pi^2 \frac{e^z}{(1+e^z)^2} +4\pi i \frac{z e^z}{(1+e^z)^2}.$$ In particular, if you tried to just evaluate the integral using that single encompassed pole's residue right away you wouldn't get the correct answer. The middle term has a contribution as well, but the third term contributes nothing.