Status of Internal websites by using PING - asp.net

Afternoon All,
Just after a bit of advice on the best method to use for the following.
I am new ish to .net and have an Asp.net web page in development that i simply lists some internal web sites by a ping command and outlines their status (on-line / offline). This is current;y activated by the click of a button.
I need to set up this developemt web page so that it automatically runs at a specific time on a morning say 7am for arguments sake and to then notify a user group by email the status of these items.
I have used Microsoft Visual studio (VB) 2010 before and can create simple web works that connect/ extract/ update data to and from SQL 2008. I have also had some experience in creating scheduled jobs in SQL but not much.
I thought i could maybe create a scheduled job in SQL 2008, find a way to populate the data into the database, use this data in my website and display it a gridview or something. And either have the SQL job or the website email a group of users the status of these internal web sites.
Does anyone know if i would beable to complete the above just in .net? Am i able to write a script of some sort or schedule the web page to run at a specified time?
Im not 100% sure on the best method to tackle this job and i have limited experience. Can anyone suggest any best method ideas on how to complete the above.
Regards
Bet.

Although fairly trivial to implement, I don't believe a ping command is useful in the context of what you are trying to achieve.
As Fredrik pointed out, a ping only says that the server is available. It makes no statement as to whether an individual website is functional on that server.
If I was doing this I would create a service that runs every so often. The service would issue a get request to the web sites, do a little bit of parsing on the content to make sure what was returned was expected, and update a record in a database stating the time of the connection and the status (up/down).
For example:
public String CheckSite(String postLocation) {
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postLocation);
// Setting the useragent seems resolves certain issues that *may* crop up depending on the server
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.KeepAlive = false;
httpRequest.Method = "GET";
using ( HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse() ) {
using ( StreamReader reader = new StreamReader(httpResponse.GetResponseStream()) ) {
result = reader.ReadToEnd();
} // using reader
} // using httpResponse
return result;
}
This simple call will load a page from a server. From there you can parse to see if you have words like "error" or what have you. Provided it looks good then you report back that the site is up.
I know the above is C#, but you should be able to easily convert that to VB if necessary. You should also place a try .. catch around the call. If it errors out then you know the server is completely offline. If the page returns, but contains "error" or something then you know the server is up but the app is down.

pesronally . . . I think the most elegant solution would be: (untested)
public String CheckSite(String postLocation) {
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postLocation);
// Setting the useragent seems resolves certain issues that *may* crop up depending on the server
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.KeepAlive = false;
httpRequest.Method = "GET";
using ( HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse() ) {
if(httpResponse.StatusCode != {check for valid status codes here})
{
//Do something based upon an invalid response.
}
} // using httpResponse
return result;
}

Related

Authentication prevents posting to Classic ASP from Web form using WebClient() from Code Behind

I am managing and old web site (site, not application) that is a hybrid of Web Forms and Classic ASP. The Classic ASP is being phased out, but that is probably a year away. Right now, we are dropping the old form of authentication in favor of Windows Authentication in the web.config.
The problem is that I am attempting to post to a Classic page from the code behind of a web form (http://www.blahsiblah.com/index.aspx) and am getting a 401 error.
var webClient = new WebClient();
var urlClassicASP = "http://www.blahsiblah.com/classic.asp";
var responseArray = webClient.UploadValues(urlClassicASP, "POST", nameValueCollection);
This throws "The remote server returned an error: (401) Unauthorized"
My question is, how can I post to the classic page without invoking the authentication of the dotNet side?
There are multiple ways to achieve this
Here is a simple suggestion that I hope helps
.Net
Use 127.0.0.1 (or your internal 192.169 / 10.1* ) IP to post to the page vs the public URL
Add a parameter (call it 'bypassauth' or something unique ) when sending the request to the ASP page
Add a parameter that identifies the user that you have authenticated in the .Net side
ASP
Find the include where the authentication check is happening and in that check, add another condition before returning 401 that checks two things
1) Request is from local/internal IP
2) Has the bypassauth parameter
3) the user id is valid
This way your old ASP code will still continue to work if requested from a browser and expect user to be authenticated however, when sending the request from .net will let you bypass authentication
I'm sure there are other ideas too, but this is just one approach
My solution was to set:
webClient.UseDefaultCredentials = true;

Client side trouble when calling

Please use my previous question as reference. I have marked an answer already.
Question
I'm a .NET developer doing some work for a company that uses Classic ASP. My experience with server side development is VB.NET or C# with some sort of MVC pattern. Consider the following code snippet, I wrote it in an ASP page the company would like to keep and "include" in other pages where this web call would be needed. Kind of like a reusable piece of code. I've left some out for sanity reasons.
//Create ActiveXObject, subscribe to event, and send request
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
xmlDoc.loadXML(xmlHttp.responseText);
debugger;
var JSON = $.xml2json(xmlDoc);
parseResponse(JSON);
}
}
urlToSend = encodeURI(REQUEST);
urlRest += urlToSend
xmlHttp.open("GET", urlRest, false);
xmlHttp.send(null);
After struggling with a variety of security problems, I was glad when this finally worked. I changed a setting in Internet Options to allow scripts to access data from other domains. I presented my solution, but the Company would have to change this setting on every machine for my script to work. Not an ideal solution. What can I do to make this run on the server instead of the client's browsers?
I have a little bit of knowledge of the <% %> syntax, but not much.
This SO Question should help you call the service server side:
Calling REST web services from a classic asp page
To Summarise, use MSXML2.ServerXMLHTTP
Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.open "GET", "Rest_URI", False
HttpReq.send
And check out these articles:
Integrating ASP.NET XML Web Services with 'Classic' ASP Applications
Consuming XML Web Services in Classic ASP
Consuming a WSDL Webservice from ASP
You will also need a way to parse JSON

msxml3.dll error '80072ee2' in ASP Page

We have just moved to a new dedicated server that has Windows 2008 and SQL Server 2008. I am trying to access an ASP page on the same server using Server.CreateObject("MSXML2.ServerXMLHTTP").
On our previous 2003 server this worked correctly, however with the new 2008 server the operation just times out.
Here is the code:
strURL = "http://www.storeboard.com/profile/profile_view.asp?MemberID=" & MemberID & "&sid=" & cSession.SessionID
Set oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
oXMLHttp.open "GET", strURL, false
oXMLHttp.send()
IF oXMLHttp.status = 200 THEN
strOut = oXMLHttp.responseText
ELSE
strOut = "Could not get XML data."
END IF
Set oXMLHttp = nothing
The code is very simple but I get the following error:
msxml3.dll error '80072ee2'
The operation timed out
/handle404.asp, line 291
Line 291 refers to oXMLHttp.Send() line.
Is there an alternative code I can use? I use the script other places on the server that access files on other servers and they work correctly, but any access to files on our server doesn't work.
Is there an alternative method that will allow me to keep the URL intact in the browser? The person could write the URL in their browser: http://www.example.com/hello the file doesn't exist but I have a 404 handler that then points the user to the correct path without changing the browser URL which is essential for our SEO ratings.
Microsoft has a published a KB article entitled INFO: Do Not Send ServerXMLHTTP or WinHTTP Requests to the Same Server
If the ServerXMLHTTP or WinHTTP component must send a request to
another ASP on the same server, the target ASP must be located in a
different virtual directory and set to run in high isolation. Avoid
using ServerXMLHTTP or WinHTTP to send a request to an ASP that is
located in the same virtual directory.
...
A finite number of worker threads (in the Inetinfo.exe or Dllhost.exe
process) is available to execute ASP pages. If all of the ASP worker
threads send HTTP requests back to the same Inetinfo.exe or
Dllhost.exe process on the server from which the requests are sent,
the Inetinfo.exe or Dllhost.exe process may deadlock or stop
responding (hang), because the pool of worker threads to process the
incoming requests will be exhausted. This is by design.
As far as alternatives go, it depends on what you're doing with the response after you receive it. If the entire purpose of the script is to forward the request to profile_view.asp, you might be able to use Server.Transfer instead.
I had this same issue. In my case the web request I was trying to make was an internal site url (within the same app pool). With server side debugging set to enabled, the asp app pool seems to be restricted to a single worker thread. By disabling this feature, the request was then able to be processed.
msxml3.dll is pretty old. It was distributed with Internet Explorer 6 to give you a rough idea.
Can you have someone install a later version on the server?
http://support.microsoft.com/kb/269238 gives you a list of versions to send to whoever it responsible for the server.
If the problem is genuinely down to a time out you could look into switching ASP buffering off. (This based soley on a guess that if the server object started to receive a response it would hold off on the timeout front.
Alternatively you coudl try processing the value on the client side, below is a function from some code I wrote which does this....
function getDets(RateID) {
var xmlHttp;
try {
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e) {
try {
// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4) {
var str;
var newStr;
str=xmlHttp.responseText
newStr=str.split("|");
window.document.all.OR2.style.display="block";
window.document.all.OR3.style.display="block";
window.document.OvertimeRates.Description.value=newStr[0];
window.document.OvertimeRates.Factor.value=newStr[1];
}
}
if (RateID==0) {
window.document.OvertimeRates.Description.value="";
window.document.OvertimeRates.Factor.value="";
}
else {
xmlHttp.open("GET","GetOvertimeRate.asp?RateID="+RateID,true);
xmlHttp.send(null);
}
}
Good luck!

looping web method until data is found and then return from web service for chat application in asp.net

I have to implement gmail style chatting in my asp.net website. now i know much has been said in this regard here and other forums...about COMET and its befits....
i recently saw this site www.indyarocks.com and when i profiled their website i found out that for chatting they send a async request and the page waits until the server has some data to return and only after the page returns....(i mean it shows status 200 OK) and again a request is dispatched.
i have implemeted chat in my website in which i poll the database after 5 sec for any new chat...so i want to know if i send a request using ASP.NET AJAX to a web method and keep on looping on the server until it has some data to return and then return to the webpage that called it is it a good approach and if not what are its demerits????
the code that i can use
<WebMethod(EnableSession:=True)> _
Public Function looper(ByVal x As String) As String
Dim flag As Boolean = False
While (flag = False)
Dim ans As String = getScalar("select 1 from Chat where sent_by=1")
If Not ans Is Nothing Then
flag = True
End If
End While
Return "x"
End Function
here i can loop over the server until it has some data
in any case is it better than the polling approach????
Does anyone have suggestions to improve this approach???
Its better than polling approach from client side
Why, because
It avoids server roundtrip - saves lot of time
And avoid unnessary calls to server (Polling approach calls the webmethod even though the data is not available)
In other hand, your current COMET approach, Server calls are minimal from javascript because the new request only be made from client if the server return the updated data.
So keep up with the current design

How to call/execute another ASP.net page from the parent ASP.Net page without disrupting the flow of events?

When a button/link is clicked, I want this URL to be called followed by the execution of the following statements.
The ASP.Net page is in C# btw.
Function A
statement A
call abc.apsx
statement B
abc.aspx is a silent page, doesn't display anything on the page but creates an output.txt file. So when abc.aspx is called, output.txt file is created and Statement B is executed seamlessly. Hope I made sense.
I have no .Net programming knowledge. Please help me.
Thank you..
You can create a HttpWebRequest object to call abc.apsx page
e.g.
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://host/abc.apsx");
or Using WebClient to fire a request to the web page.
WebClient client = new WebClient ();
// Add a user agent header in case the
// requested URI contains a query.
// important to add user-agent to emulate a real request from browser.
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead ("http://host/abc.apsx");
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
Console.WriteLine (s);
data.Close ();
reader.Close ();
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx
This is exactly what HttpServerUtility.Execute is for.
Why are you having the abc.aspx act as a standalone page?
From how your question reads, you're goal is to get the Output.Txt file, thus, why not have a class that builds that output.txt file as a separate object which your initial page can call?
And then, if you need it accessible to the user, have abc.aspx call this class as well.
Or you can go with codemeit's suggestion of the httpwebreques (which if abc.aspx is on a separate domain this is probably your best course of action)

Resources