We are upgrading our application from .net framework to .net core 2.0.
In it, we use a HttpWebRequest
to contact a site with AllowAutoRedirect
set to false. When the code executed request.GetResponse()
The site will return a 302 response, which in .net framework is OK - you can take response and handle it (we are after the set-cookie
header value).
However, in .net core 2.0, a WebException gets thrown:
The remote server returned an error: (302) Found.
Is my understanding incorrect in that a 302 should result in an exception being thrown, rather if AllowAutoRedirect is set to false then the response should still be returned? Is there any way to trigger the same behavior experienced in .net framework?
I got the same error when setting AllowAutoRedirect
to false
.
I solved the problem by just wrapping a try-catch
-block around request.GetResponse()
and assigning the Result of the exception to a variable
WebResponse response;
try {
response = request.GetResponse();
}
catch(WebException e)) {
if(e.Message.Contains("302")
response = e.Result;
}