Showing posts with label httpwebrequest. Show all posts
Showing posts with label httpwebrequest. Show all posts

Monday, August 29, 2011

ASP.NET: Automatically convert http urls into https url

Sometimes we need to navigate securely within the website because of some confidential information. The user might use just http but we need to change it into https. So the approach I like best is to do it in masterpage level. We can write a small code snippet in the page load section of the masterpage which checks if the page is http or not. If it is http then changes it to https. Following are the line of codes that we need to put in the master page's page load function. I did not come up with these code, I collected it from the internet. So I am not taking any credit for the code ;)

//Redirect to https if typed http.

string redirectUrl = null;
HttpContext context = HttpContext.Current;

// if we want HTTPS and it is currently HTTP
if (!context.Request.IsSecureConnection)
{
redirectUrl = context.Request.Url.ToString().Replace("http:", "https:");
}

// check if we need to redirect, and if so use redirectUrl to do the job
if (redirectUrl != null)
context.Response.Redirect(redirectUrl);

Hope it helps. :)

Thursday, March 3, 2011

ASP.net : Post data to another page programmatically without a form submit button.

I was working on a project where I had to pass some value securely to a php script from an ASP.NET page. I found that .NET web request and web response class is there to do this task. Here is the MSDN provided tutorial on how to use these two classes to sent the data through post method and get the response.

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

In the receiving page, I need to write the data through Response.write (for ASP.net) or echo(for php). The response is automatically sent to the requesting page.

Is not it cool!!

** I believe when there are so many quality tutorials over the web, I do not really need to write one of my own unless the topic is something unique**