Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

access web page to check for availibility 1

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hello
I need to access a website so often to make sure it is up and available to users. (Long story short, it is a 1.1 web app running on windows 2003 and for some reason, the site keeps betting put in app pools that cause it to break). So I have a service that runs the following code:
Code:
string lstrURL = Convert.ToString(ldrAlert["USER_01_VALUE"], System.Globalization.CultureInfo.CurrentCulture);
string lstrPageContent = "Empty";
HttpWebRequest lwhwrWebRequestObject ;
try
{
	lwhwrWebRequestObject = (HttpWebRequest)HttpWebRequest.Create(lstrURL);
	if (!lwhwrWebRequestObject.HaveResponse)
	{
		throw new System.Net.WebException();
	}
}
 catch (System.Net.WebException)
{
	TODO:SEND ME AN EMAIL
}
finally
{
	lwhwrWebRequestObject = null;
}

I always end of getting an email even if the site is up. I can access the same website on the computer this service is running on using IE but in code I aways seem to not be able to connect. I also had this code

Code:
lwhwrWebRequestObject.GetResponse()

When I do this, I would get a TimeOut error. I am not sure why. According to the MSDN the HttpWebRequest.TimeOut default value is 100 seconds, but I get an error with-in 10.

Any clue how I can check for the sites availability if this is not the most accurate way or any idea why I can not get this code to execute correctly?

Thanks,
RalphTrent
 
Here is some more info...
this only seems to be an issue if I am executing this code in a Service, as a standalone console app, the problem does not occur.

Any idea's?
 
Code:
if (!lwhwrWebRequestObject.HaveResponse)
    {
        throw new System.Net.WebException();
    }
}
 catch (System.Net.WebException)
{
    TODO:SEND ME AN EMAIL
}
this is terrible code. don't check the status, to throw an exception, only to catch it immediately. instead do something like this
Code:
var log = LogManager.GetLogger(...);
try
{
   response = (HttpWebRequest)HttpWebRequest.Create(url);
   if (response.HaveResponse == false)
   {
      log.Warn("site {0} is down", url);
   }
}
catch(Exception e)
{
   log.Error(e.Message, e);
}
where log manager is part of the log4net library. you can then configure log4net to send the message to multiple locations.

as for why it's failing... does the site require authentication? if so then this may be the problem. by default services run under the local system account which has very limited rights. if you need to authenticate to the website you will need to pass credentials through the http request.

this isn't a problem for the console application because the console application runs under your credentials.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Yeah, this is pretty sloppy code. This was my "kitchen sink" attempt. I have tried pretty much evertying else and I just posted the wrong sample.

I will give your idea a try.

As far as website security, it uses windows authentication. I will try to run the service as myself to see if that has any impact as you are implying.

Thanks,
RalphTrent
 
J,
"Your right, and when your right, your right. And you, your always right." (John Candy -- Spaceballs)

It was the user issue. I assume this is because the site is set to Windows Authentication but the service is running as a LocalSystem user. I am not sure how that exactly does not seem to work well with one another, but I ran the service as myself and it worked.

Now is there a way to keep the service running as localsystem and still have this work? I know you said I can pass credentials to the request but I do not want to have to maintain a user account for this service.

Thanks again,
RalphTrent
 
I love the quote. Spaceballs is a great comedy.
I assume this is because the site is set to Windows Authentication but the service is running as a LocalSystem user.
correct.
I am not sure how that exactly does not seem to work well with one another, but I ran the service as myself and it worked.
I'm not 100% sure about this either. You should get a response, just not a successful response. The Http Status code would probably be something other than 200.

Now is there a way to keep the service running as localsystem and still have this work? I know you said I can pass credentials to the request but I do not want to have to maintain a user account for this service.
Why not configure the service to run as an existing user? then the credentials are passed automatically

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Lone Star : So what does that make us?
Dark Helmut: ABSOLUTLY NOTHING!
Why not configure the service to run as an existing user? then the credentials are passed automatically
I could do this, but when I would have to change the password on the service everytime the password changes to the user. UNLESS I create a local user. That might work. Ill try that.

to give you the background on why I am doing this. I have 1.1 asp.net sites and sometimes they get grouped into AppPools (iis 7) with sites that are not 1.1. when that happens, my sites become defunct. No one will take responsiblity for the server so I want to right a service to check the site every 15 minutes to make sure the site is up and running.

Ill try the LocalUser idea.
 
why not create a separate application pool for .net 1.1 projects? I'm running IIS6 in production and I create an application pool for each site I design. this way application A doesn't interfere with application B.
What the hell am I look at?
You're looking at now sir.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thats exactly what I do have but some people get in on the box and mess it up (again no one takes responsiblity)

No I did not see you playing with your dolls sir!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top