473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Testing if URL exists and then redirecting to another page if fail

I have had no luck with the following scenario:

One of our requirements for a particular business scenario is to test the
existance of a URL before sending the client there. If the URL does not
exist, then we send them to another page.

To test the destination page, I use this code:

bool foundUrl = true;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
try
{
request.Method = "HEAD";
request.GetResponse();

//no probs so continue to destination
HttpContext.Current.Response.Redirect(destinationU rl, true);
}
catch (WebException webEx)
{
foundUrl = false;
}
finally
{
if (requestCheck != null)
{
requestCheck.Abort();
}
}

The problem I have now is that Response is stuck in Status 404. It will not
allow me to Redirect. I have tried the following workarounds to no avail:

1) Response.Write("<script
language=javascript>window.navigate('mynewpage.asp x');</script>")
Response.End

2)
Response.ClearHeaders();
Response.ClearContent();
Response.Redirect('mynewpage.aspx');
Nov 9 '07 #1
3 3171
Noremac wrote:
I have had no luck with the following scenario:

One of our requirements for a particular business scenario is to test the
existance of a URL before sending the client there. If the URL does not
exist, then we send them to another page.

To test the destination page, I use this code:

bool foundUrl = true;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
try
{
request.Method = "HEAD";
request.GetResponse();

//no probs so continue to destination
HttpContext.Current.Response.Redirect(destinationU rl, true);
}
catch (WebException webEx)
{
foundUrl = false;
}
finally
{
if (requestCheck != null)
{
requestCheck.Abort();
}
}

The problem I have now is that Response is stuck in Status 404.
How have you come to that conclusion? What happens when you try? Do you
get any error message?

There is no reason that the HttpWebRequest object would affect the
current response in any way.
It will not
allow me to Redirect. I have tried the following workarounds to no avail:

1) Response.Write("<script
language=javascript>window.navigate('mynewpage.asp x');</script>")
Response.End

2)
Response.ClearHeaders();
Response.ClearContent();
Response.Redirect('mynewpage.aspx');

--
Göran Andersson
_____
http://www.guffa.com
Nov 10 '07 #2
Hi Noremac,

I'm not sure if I fully understand your question: do you mean that after
requesting an existing page, the statement
"HttpContext.Current.Response.Redirect..." will fail? I cannot reproduce
the issue on my side using a simple webform. Maybe it's related to your
other code or specific configuration?
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 12 '07 #3
Yep, after I had some time today to finally throw the code in a test app it
worked for me too. Went back to my real code and it turned out a wrapper
object to HttpContext.Current was wrong. Now works for me too.

""Walter Wang [MSFT]"" wrote:
Hi Noremac,

I'm not sure if I fully understand your question: do you mean that after
requesting an existing page, the statement
"HttpContext.Current.Response.Redirect..." will fail? I cannot reproduce
the issue on my side using a simple webform. Maybe it's related to your
other code or specific configuration?
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Edvard Majakari | last post by:
Hi all ya unit-testing experts there :) Code I'm working on has to parse large and complex files and detect equally complex and large amount of errors before the contents of the file is fed to...
6
by: Shabam | last post by:
A web application of mine developed using C# + MS SQL runs fine normally. However when I stress test it with a load testing software (using about 60 simultaneous users) some instances start...
3
by: Jim Hunter | last post by:
From what I've read, the only indication fprintf gives of an error is a negative return value. I have a series of writes to a file using fprintf, and, while I need to know if a failure has...
1
by: Bilbo | last post by:
Hello, How do I programatically redirect a page in "another frame" using C# in ASP.NET? Server.Transfer redirects the current page...not a different frame. Thanks.
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
4
by: Karim | last post by:
Hi, I need to write some code to figure out if a file exists before I overwrite it. I came up with this code, but not sure its the best.. bool isImgCached; TFilePtr pCachedFile =...
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
5
by: Gargoyle | last post by:
Is there a way in JavaScript to test if a webpage exists, for example before redirecting a browser window to that page? What I am trying to do is customize an acknowledgement page for particular...
6
by: Strato | last post by:
Hi folks, I want to write some kind of test to check at startup if another instance of my script is already running. I don't want to handle writing of a PID file because it is too Unix/Linux...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.