.NET Core 中 WebRequest 的重定向
Redirect of WebRequest in .NET Core
我使用 .NET 4.7.1 在 C# 中编写了以下程序:
var req = (HttpWebRequest) WebRequest.Create(myUrl);
req.AllowAutoRedirect = false;
var rsp = req.GetResponse();
Console.WriteLine(rsp.Headers["Location"]);
我请求的网站正在返回 301 响应,并且“位置”header 包含要重定向到的 URL。
如果我使用 .NET Core 2.1 做完全相同的事情,我会从对 GetResponse
的调用中抛出一个 WebException
。我怎样才能避免这种情况?
基于this,您需要将其捕获在try/catch
块中并检查WebException
:
If you set AllowAutoRedirect
, then you will end up not following the
redirect. That means ending up with the 301 response. HttpWebRequest
(unlike HttpClient
) throws exceptions for non-successful (non-200)
status codes. So, getting an exception (most likely a WebException
) is
expected. So, if you need to handle that redirect (which is HTTPS ->
HTTP by the way), you need to trap it in try/catch block and inspect
the WebException etc. That is standard use of HttpWebRequest
.
That is why we recommend devs use HttpClient
which has an easier use
pattern.
像这样:
WebResponse rsp;
try
{
rsp = req.GetResponse();
}
catch(WebException ex)
{
if(ex.Message.Contains("301"))
rsp = ex.Result;
}
我在处理它时几乎已经弄明白了,但我想我会 post 在这里以防其他人 运行 遇到同样的问题。
响应作为抛出的异常的一部分包含在内,因此我能够通过将我的代码修改为以下内容来获得与 .NET 4.7.1 中相同的行为:
var req = (HttpWebRequest) WebRequest.Create(myUrl);
req.AllowAutoRedirect = false;
try
{
var rsp = req.GetResponse();
Console.WriteLine(rsp.Headers["Location"]);
}
catch (WebException e)
{
var rsp = (HttpWebResponse) e.Response;
if (rsp.StatusCode == HttpStatusCode.Moved ||
rsp.StatusCode == HttpStatusCode.MovedPermanently ||
rsp.StatusCode == HttpStatusCode.Found)
{
Console.WriteLine(rsp.Headers["Location"]);
}
else throw;
}
我使用 .NET 4.7.1 在 C# 中编写了以下程序:
var req = (HttpWebRequest) WebRequest.Create(myUrl);
req.AllowAutoRedirect = false;
var rsp = req.GetResponse();
Console.WriteLine(rsp.Headers["Location"]);
我请求的网站正在返回 301 响应,并且“位置”header 包含要重定向到的 URL。
如果我使用 .NET Core 2.1 做完全相同的事情,我会从对 GetResponse
的调用中抛出一个 WebException
。我怎样才能避免这种情况?
基于this,您需要将其捕获在try/catch
块中并检查WebException
:
If you set
AllowAutoRedirect
, then you will end up not following the redirect. That means ending up with the 301 response.HttpWebRequest
(unlikeHttpClient
) throws exceptions for non-successful (non-200) status codes. So, getting an exception (most likely aWebException
) is expected. So, if you need to handle that redirect (which is HTTPS -> HTTP by the way), you need to trap it in try/catch block and inspect the WebException etc. That is standard use ofHttpWebRequest
.That is why we recommend devs use
HttpClient
which has an easier use pattern.
像这样:
WebResponse rsp;
try
{
rsp = req.GetResponse();
}
catch(WebException ex)
{
if(ex.Message.Contains("301"))
rsp = ex.Result;
}
我在处理它时几乎已经弄明白了,但我想我会 post 在这里以防其他人 运行 遇到同样的问题。
响应作为抛出的异常的一部分包含在内,因此我能够通过将我的代码修改为以下内容来获得与 .NET 4.7.1 中相同的行为:
var req = (HttpWebRequest) WebRequest.Create(myUrl);
req.AllowAutoRedirect = false;
try
{
var rsp = req.GetResponse();
Console.WriteLine(rsp.Headers["Location"]);
}
catch (WebException e)
{
var rsp = (HttpWebResponse) e.Response;
if (rsp.StatusCode == HttpStatusCode.Moved ||
rsp.StatusCode == HttpStatusCode.MovedPermanently ||
rsp.StatusCode == HttpStatusCode.Found)
{
Console.WriteLine(rsp.Headers["Location"]);
}
else throw;
}