Integral $\int_0^\infty \frac{x^{2k}}{x^2+1}dx$ I am little bit confused by the following integral:
$$\int_0^\infty \frac{x^{2k}}{x^2+1}dx,$$
which according to WA is equal to
$$\int_0^\infty \frac{x^{2k}}{x^2+1}dx=\frac{\pi}{2}\sec(\pi k),\quad \text{for}\ \operatorname{Re}(k)>-\frac{1}{2}.$$
However, by plugging $k=1,2,...$, to RHS, this should be equal to $\frac{\pi}{2}(-1)^k$.
On the other side, plugging $k=1,2,...$ to LHS, these integrals should not exist. Since I could not derive that result, I would like to know what is going on here? Maybe WA evaluates is wrongly? Thanks for any hint.

Letting $\displaystyle \frac{1}{t}=x^{m}+1$, then $
\displaystyle d x=\frac{1}{m} \left(\frac{1}{t}-1\right)^{\frac{1}{m}-1}\left(-\frac{1}{t^{2}}\right) d t.
$
Consequently
$$
\begin{aligned}
\int_{0}^{\infty} \frac{x^{r}}{x^{m}+1} d x &=-\int_{1}^{0} \frac{\left(\frac{1}{t}-1\right)^{\frac{r}{m}}}{\frac{1}{t}} \frac{1}{m t^{2}}\left(\frac{1}{t}-1\right)^{\frac{1}{m}-1} d t \\
&=\frac{1}{m} \int_{0}^{1} \frac{(1-t)^{\frac{r}{m}}(1-t)^{\frac{1}{m}-1}}{t^{\frac{r+1}{m}}} d t \\
&=\frac{1}{m} \int_{0}^{1} t^{-\frac{r+1}{m}}(1-t)^{\frac{r+1}{m}-1} d t \\
&=\frac{1}{m} B\left(1-\frac{r+1}{m}, \frac{r+1}{m}\right)
\end{aligned}
$$
By the property of Beta function,
$$
B(x, y)=\frac{\Gamma(x) \Gamma(y)}{\Gamma(x+y)},
$$
where $\operatorname{Re}(x)>0$ and $\operatorname{Re}(y)>0,$
we have
$$
\int_{0}^{\infty} \frac{x^{r}}{x^{m}+1} d x=\frac{\Gamma\left(1-\frac{r+1}{m}\right) \Gamma\left(\frac{r+1}{m}\right)}{m\Gamma(1)}
$$
Using the Euler’s Reflection Theorem,
$$
\Gamma(1-z) \Gamma(z)=\pi \csc (\pi z),
$$
where $z\notin Z$,
we can now conclude that
$$\boxed{\int_{0}^{\infty} \frac{x^{r}}{x^{m}+1} d x=\frac{\pi}{m} \csc \frac{(r+1) \pi}{m}}$$
where $m>r+1>0$.
In particular, when $r=2k$ and $m=2$,
$$\int_{0}^{\infty} \frac{x^{2k}}{x^{2}+1} d x=\frac{\pi}{2} \csc \frac{(2k+1) \pi}{2}=\frac{\pi}{2} \sec (k\pi)$$
However, the property of Beta function,
$$
B(x, y)=\frac{\Gamma(x) \Gamma(y)}{\Gamma(x+y)},
$$
holds for $\operatorname{Re}(x)>0$ and $\operatorname{Re}(y)>0,$
if and only if $1-\frac{2 k+1}{2}>0 \Leftrightarrow k<\frac{1}{2}$.
Therefore for any $k<\frac{1}{2}$,
$$\int_{0}^{\infty} \frac{x^{2k}}{x^{2}+1} d x=\frac{\pi}{2} \csc \frac{(2k+1) \pi}{2}=\frac{\pi}{2} \sec (k\pi) \tag*{(*)}  $$
but doesn’t hold for all $k\geq \frac{1}{2}, $ which explains why we can’t put $k=1,2,3, \cdots$ into $(*)$.
