Evaluating $\int_{-\infty}^{\infty}\frac{\cos x}{e^x + e^{-x}}$ using the Residue Theorem I consider the complexification $$f(z)=\frac{e^{iz}}{e^z+e^{-z}}$$ 
Poles of $f$: $\text{Denominator}=e^{-z}(e^{2z}+1)=0\Rightarrow e^{2z}=-1=e^{i(\pi + 2\pi k)}\Rightarrow z=\frac{i\pi(1+  2k)}{2}$, so the poles lie on the $y-$axis. Since there are infinitely many of them, one contour  I would like to use is a semicircle of infinite radius, but I am not sure if I am allowed to use such a contour when applying the residue theorem. Another contour that comes to mind is the boundary of a rectangle with vertices $(-R,0),(R,0),(R,\frac{\pi i}{4}), (-R,\frac{\pi i}{4})$, but 3 of these integrals seem difficult to calculate. I am not sure if the integrals along the opposite vertical sides will cancel each other out. How can I proceed? I have more experience with integrating along circular arcs than lines.
 A: Much easier to use a rectangular contour $C$, in this case, vertices at $-R$, $R$, $R+i \pi$, $-R + i \pi$.  Then consider
$$\frac12 \oint_C dz \frac{\cos{z}}{\cosh{z}} $$
which is equal to
$$\frac12 \int_{-R}^R dx \frac{\cos{x}}{\cosh{x}} + i \frac12\int_0^\pi dy \frac{\cos{(R+i y)}}{\cosh{(R+i y)}} \\ + \frac12 \int_R^{-R} dx \frac{\cos{(x+i \pi)}}{\cosh{(x+i \pi)}} + i \frac12 \int_{\pi}^0 dy \frac{\cos{(-R+i y)}}{\cosh{(-R+i y)}}$$
Now,
$$\cosh{(x + i \pi)} = -\cosh{x}$$
$$\cos{(x+i \pi)} = \cosh{\pi} \cos{x} - i \sinh{\pi} \sin{x} $$
We take the limit as $R \to \infty$.  In this limit, the second and fourth integrals vanish and the contour integral is equal to
$$\frac{1+\cosh{\pi}}{2} \int_{-\infty}^{\infty} dx \frac{\cos{x}}{\cosh{x}} - i \frac{\sinh{\pi}}{2} \int_{-\infty}^{\infty} dx \frac{\sin{x}}{\cosh{x}}$$
The imaginary part is zero because the corresponding integral is zero.
By the residue theorem, the contour integral is equal to $i 2 \pi$ times the residue of the integrand at the pole $z=i \pi/2$, or
$$i  2 \pi \frac12 \frac{\cosh{(\pi/2)}}{i \sin{(\pi/2)}} = \pi \cosh{(\pi/2)} $$
Thus,
$$\int_{-\infty}^{\infty} dx \frac{\cos{x}}{e^x+e^{-x}} = \frac{\pi \cosh{(\pi/2)}}{1+\cosh{\pi}} = \frac{\pi}{2 \cosh{(\pi/2)}}$$
A: To see it done with the semicircle, let
$$I = \displaystyle\int_{-\infty}^{\infty} \dfrac{cos x}{e^x+e^{-x}}dx$$
$$J = \displaystyle\oint_L \dfrac{e^{iz}}{e^z+e^{-z}}dz$$
Where $L$ is a semicircle of radius $R$ in the upper half plane s.t. exactly $r$ of the poles are enclosed (i.e. L encloses poles $z=(2k+1)\dfrac{i\pi}{2}$ for $k=0,…,r-1$) and none lie on the boundary.
Then, as $R\to \infty$,
$$\text{Integral along real axis} \ \to \displaystyle\int_{-\infty}^{\infty} \dfrac{e^{ix}}{e^x + e^{-x}}dx $$
And since $|z|\left|\dfrac{e^{iz}}{e^z+e^{-z}}\right|\sim \left|\dfrac{z}{e^z}\right|\to 0 \ \ \text{as} \ \ |z|\to \infty$,
$$\text{Integral along circular arc} \ \to 0 $$
So,
$$ J\to \displaystyle\int_{-\infty}^{\infty} \dfrac{e^{ix}}{e^x+e^{-x}}dx \ \ \text{as} \ \ R\to \infty$$
Furthermore, by the residue theorem:
$$J = 2\pi i \displaystyle\sum_{k=0}^{r-1} \text{Res}\left[\dfrac{e^{iz}}{e^z+e^{-z}} \ ,\  z=(2k+1)\dfrac{i\pi}{2}\right] $$
By L'Hopital,
$$ = \pi  \displaystyle\sum_{k=0}^{r-1} \dfrac{e^{-(2k+1)\pi/2}}{\sin [(2k+1)\pi/2]} $$ 
$$ = \dfrac{\pi}{e^{\pi/2}}\displaystyle\sum_{k=0}^{r-1} (-e^{-\pi})^k$$
$$ \to \dfrac{\pi}{e^{\pi/2}}\dfrac{1}{1+e^{-\pi}}$$ 
As $R\to \infty$, so,
$$J = \dfrac{\pi}{2\cosh (\pi/2)}$$
And finally,
$$I=\Re J \implies \displaystyle\int_{-\infty}^{\infty} \dfrac{cos x}{e^x+e^{-x}}dx  = \dfrac{\pi}{2\cosh(\pi /2)}$$
A: You may use:
$$ I = \int_{\mathbb{R}}\frac{\cos x}{e^{x}+e^{-x}}\,dx = 2\sum_{n\geq 0}(-1)^n \int_{0}^{+\infty}e^{-(2n+1)x}\cos x\,dx = 2 \sum_{n\geq 0}\frac{(-1)^n(2n+1)}{1+(2n+1)^2}$$
then compute the residues of $\frac{z}{\cosh\frac{z}{2}}$ at the odd multiples of $i\pi$ in order to prove that the last series equals $\displaystyle\color{red}{\frac{\pi}{e^{\pi/2}+e^{-\pi/2}}}$.
