2

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();
}
N. Saw
  • 103
  • 2
  • 7
  • A couple of doubts. What is your client? Is it an API? because the client-side code is also in C#. Second, if it is an API then what kind of redirect you want? – Ankush Jain Mar 07 '19 at 11:06
  • Yes. The client is a desktop app in c# and the server is an API in c#. I want to redirect the request to another API server. Without changing the method ("Post" should be "Post"). – N. Saw Mar 07 '19 at 11:17
  • You can have a base url in your desktop application. Just update it's value accrodingly. – Ankush Jain Mar 07 '19 at 11:41
  • Temporary redirection can occur at any time. And it may not happen. If the client receives 307 in response, it must redirect the request to another server. Otherwise, it receives and processes the response from the first server. This should be automatic. – N. Saw Mar 07 '19 at 13:24
  • I tried to redirect by changing the base URL, but it requires exception handling and sending the request again. It works. However, I am looking for a way to do this without exception, but using headers. To redirect automatically. – N. Saw Mar 07 '19 at 13:37
  • https://stackoverflow.com/questions/45603984/httpwebrequest-in-net-core-2-0-throwing-302-found-exception maybe help – Marcelo Vismari Mar 07 '19 at 14:00

1 Answers1

0

In my case I get the same status code 307. The solution was a combination of this answer: https://stackoverflow.com/a/46257876/9674093 and the right redirect-link from WebException. The right link was in the WebHeaderCollection of the e.Response under "Location".

To get it you can use this (in my case it was a value 4):

Uri Link = new Uri(e.Response.Headers.Get(4));

or something like this:

Uri Link = new Uri(e.Response.Headers.Get("Location"));

Example:

WebResponse response;
        try {
           response = GetWebResponse(your_url);
        }
        catch(WebException e)) {
           if(e.Message.Contains("307")
             {
             //Get redirect-link from e.Response.Header.Location
             Uri Link = new Uri(e.Response.Headers.Get(4));
             // Start second request
             var second_request = GetWebRequest(Link);
             response = GetWebResponse(second_request);
             }
        }
                    
Alexander S.
  • 1,971
  • 2
  • 14
  • 22