Pure Redirection using WebRequest and WebResponse

RIn ASP.NET C# general method which is used to redirect from one page to another is Response.Redirect("url").
But a smart user can also navigate to a page by simply entering URL in address bar.
 
There may be some cases where you are not suppose to allow user to hit a page by directly entering URL.
In such cases a feasible  way to implementation is use WebRequest and WebResponse
 
Below is simple code for above scenario.
 
 Button event on click of which you want to jump to URL:
  1. protected void btnRedirect_Click(object sender, EventArgs e)  
  2. {  
  3.       WebRequest req = null;  
  4.       WebResponse rsp = null;  
  5.       string  xmlString = @"<request>Some data</request>";  
  6.       //It is not mandatory to pass any request parameter.  
  7.    
  8.       req = WebRequest.Create("URL"); //URL of desired page can be entered here  
  9.       req.Method = "POST"//OR GET  
  10.       req.ContentType = "text/xml"//OR json  
  11.    
  12.       StreamWriter writer = new StreamWriter(req.GetRequestStream());  
  13.       writer.WriteLine(xmlString);  
  14.       writer.Close();  
  15.    
  16.       rsp = req.GetResponse();  
  17.    
  18. }  
 Page load event on which the request is to be handled: 
  1. protected void Page_Load(object sender, EventArgs e)    
  2. {    
  3.       Page.Response.ContentType = "text/xml";  
  4.       StreamReader reader = new StreamReader(Page.Request.InputStream);  
  5.       string  xmlRequestString = reader.ReadToEnd();  
  6.       XDocument doc = new XDocument.Parse(xmlRequestString);   
  7.       string reqData = doc.Root.Element("request").Value;   
  8.       //  
  9.       //Handle the conditions you want to handle here.  
  10.       //   
  11. }    
In this way, the desired results can be achieved.
 
For any queries please do contact.
 
Thank you. 
Ebook Download
View all
Learn
View all