Asked by:
AllowAutoRedirect = true is not redirecting in HttpWebRequest

Question
-
User430178104 posted
Hi,
I am using the below code to Login and redirect the another domain site. Unfortunately it is not redirecting instead it is giving the html content. Can any one please help on this
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://........:8080/Master/"); request.Method = "POST"; request.UseDefaultCredentials = false; request.PreAuthenticate = true; request.Credentials = new NetworkCredential("san", "************"); request.AllowAutoRedirect = true; request.CookieContainer = new CookieContainer(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Tuesday, July 11, 2017 11:45 AM
All replies
-
User753101303 posted
Hi,
My first move would be to check the header and the status code. This is a server side redirect rather than a JavaScript redirection?
Ah you don't expect the redirection to happen in the browser? For now this is a server side request and so redirection won't have any effect on your own client side browser if this is what you meant...
Tuesday, July 11, 2017 12:03 PM -
User-707554951 posted
Hi pathipati,
HttpWebRequest.AllowAutoRedirect Property indicates whether the request should follow redirection responses. if you want the request to automatically follow HTTP redirection headers to the new location of the resource, you could set AllowAutoRedirect to true.
If AllowAutoRedirect is set to false, all responses with an HTTP status code from 300 to 399 is returned to the application.
For example, in CS.aspx.cs redirect to another page (PageB.aspx) :
public partial class CS : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Redirect("~/Test/PageB.aspx"); } }
Then I use the following code, The result is the content of PageB.aspx.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:52634/Test/CS.aspx"); request.Method = "POST"; request.UseDefaultCredentials = false; request.PreAuthenticate = true; //NetworkCredential cred = new NetworkCredential("san", "************"); //CredentialCache cache = new CredentialCache(); //cache.Add( new Uri("http://........:8080/Master/"), "Ntlm", cred ); // request.Credentials = cache; request.AllowAutoRedirect = true; request.ContentLength = 0; request.CookieContainer = new CookieContainer(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); string html = streamReader.ReadToEnd();
Output:
For more information, please refer to the following links:
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowautoredirect(v=vs.110).aspx
Best regards
Cathy
Wednesday, July 12, 2017 3:39 AM -
User430168141 posted
The question is old, but still want to add the answer for anyone looking this up in the future.
I had the same issue, reason have been multiple redirects in the background.
Example:
- Requested URL: https://example.com//bla Error was multiple / after the .com => 301 redirect
- Redirect to http://example.com/bla No double / anymore, but lost html
- Redirect to https://example.com/bla So basically it was a mix of serverside (redirect to non https) and client-side issue (wrong URL)
So simply check your calls with the network tab in the developer tool of your browser (like F12 in Chrome)
Thursday, January 21, 2021 9:22 AM