Change server IPAddress and perform a redirection automatically
I have created an web based application through which the users can change the server (where the web application is hosted) IPAddress.
The problem is that, once i have changed the IPAddress to a new IPAddress, Response.Redirect("MyHome.aspx") is not working any more. I have also tried to redirect the user to the newly updated address but even it doesn't do the trick. No page found message appears after some time.
For example: The url while the web application runs in IIS is : http://192.168.0.65/WebDemo/Default.aspx after changing the 开发者_开发技巧IPAddress to 192.168.0.66 and redirecting it with the Response.Redirect() method the http://192.168.0.65 is not accessible.
Any idea of achieving this task of changing IPAddress of the server and doing an automatic redirect to the newly assigned IPAddress?
When the browser sends the request to change the IP address, a TCP session is created between the client and your server. The browser waits on that connection for a response.
Changing the IP address of the server breaks the session, so the server cannot send data back over that link.
You would need to send the redirect before you actually change the IP address. (Better: send a page with a timed redirect using javascript or a meta refresh or something.)
First of all thanks to Mat for his advice. I am posting the solution here to help other programmers facing the same problem.
I have solved the problem of redirecting by changing the IP address in the code behind of my page lets say it is http://192.168.0.160/ChangeIp.aspx and then after changing the IP address i perform redirection to an intermediate page lets name it Redirector.aspx as shown in the code below:
// Code presenet in button click handler og ChangeIP.aspx page
protected void btnSaveAndRedirect_Click(object sender, EventArgs e)
{
this.ChangeIPAddress();
// Pass the new IP address via Query String to the Redirector.aspx page
Response.Redirect("Redirector.aspx?ip=" + this.txtIPAddress.Text);
}
Redirector.aspx is a simple page containing a image to indicate "Redirecting please wait.." and a TextBox which contains the new IP address where redirection need to be made. The html of these elements are present in Redirector.aspx page as shown below:
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td class="style1"><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td><img src="Images/Transparent.png" alt="" /></td>
</tr>
<tr>
<td align="center"><img src="Images/Redirect.gif" alt="Please wait while redirecting" /></td>
</tr>
</table>
</div>
<p>
<asp:TextBox ID="txtIP" runat="server"></asp:TextBox>
</p>
</form>
Note that textbox's text property gets changed in the page load event as shown below:
protected void Page_Load(object sender, EventArgs e)
{
this.txtIP.Text = Request.QueryString["ip"];
}
and then in this aspx page i have created a timer of five seconds to redirect to the newly assigned IP as shown in the code below:
<SCRIPT language="JavaScript" type="text/javascript">
<!--
var secs
var timerID = null
var timerRunning = false
var delay = 1000
function InitializeTimer()
{
// Set the length of the timer, in seconds
secs = 5
StopTheClock()
StartTheTimer()
}
function StopTheClock()
{
if(timerRunning)
clearTimeout(timerID)
timerRunning = false
}
function StartTheTimer()
{
if (secs==0)
{
StopTheClock()
// Here's where you put something useful that's
// supposed to happen after the allotted time.
var ip = document.getElementById('<%= txtIP.ClientID %>').value;
window.location.href = "http://" + ip;
}
else
{
self.status = secs
secs = secs - 1
timerRunning = true
timerID = self.setTimeout("StartTheTimer()", delay)
}
}
window.onload = function() {
// Hide textbox from user
document.getElementById('<%= txtIP.ClientID %>').style.display = 'none';
InitializeTimer();
}
//-->
</SCRIPT>
Now after arriving at Redirector.aspx page the page gets automatically redirected to the new IP address after five seconds. Hope this logic will help someone out there.
精彩评论