I need to set up a temporary automatic redirection of client requests from one server to another. From the server side:
string url = Constants.UrlRedirect;
Response.Headers.Add("Location", url);
return StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status307TemporaryRedirect);
However, from the client’s side, automatic redirection does not occur, and the process falls into exception (status code 307). I can serve a redirect through the WebException, but I don't want to. How to set up an automatic redirect? Please help. Client side code:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
{
string json = JsonConvert.SerializeObject(post);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();
string result = string.Empty;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}