Reputation: 101
I'm trying to check if a website is online and taking urls from mssql server 2008 and if said website is not working, my code should send an e-mail.
It is working fine there is no problem with taking data from server but if I try to check a not working website for example http://blabalabalabal.com
it gives me an error like: Object reference not set to an instance of an object.
on this line:if (response.StatusCode != HttpStatusCode.OK)
How can I solve this?
string connectionString = "Data Source=eur-vpc\\sqlexpress;Integrated Security=True;Initial Catalog=Website";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SiteeeID = Convert.ToInt32(listBox1.SelectedValue);
string url = "select http from AyrintiSite where SiteID='" + SiteeeID + "'";
connection.Close();
ArrayList Sites = new ArrayList();
using (SqlConnection cn = new SqlConnection(connectionString))
{
using (SqlCommand cm = new SqlCommand(url, cn))
{
cn.Open();
SqlDataReader reader = cm.ExecuteReader();
while (reader.Read())
{
Sites.Add(reader.GetString(0));
}
}
}
for (int i = 0; i < Sites.Count; i++)
{
string url2 = Sites[i].ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url2));
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// string gelenYanit = response.StatusCode.ToString();
var response = GetResponse(request);
if (response.StatusCode != HttpStatusCode.OK)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Website";
mail.Body += "Website is not working";
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "******");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail send");
}
}
}
private HttpWebResponse GetResponse(HttpWebRequest request)
{
try
{
return (HttpWebResponse)request.GetResponse();
}
catch (System.Net.WebException e)
{
if (e.Response == null)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Website";
mail.Body += "Website is not working";
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "********");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail send");
}
return (HttpWebResponse)e.Response;
}
Upvotes: 0
Views: 1192
Reputation: 6046
Rather than returning an invalid status code directly, the GetResponse
method likely throws an System.Net.WebException
, whereas the WebException
contains the actual error code/response. Therefore, wrap the GetResponse
call in a try-catch block, or even wrap that in a method:
void YourMethod()
{
string url2 = Sites[i].ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url2));
HttpWebResponse response;
string errorMessage;
var gotResponse = TryGetResponse(request, out response, out errorMessage); // This is now a safe call
if (!gotResponse || response.StatusCode != HttpStatusCode.OK)
{
// Send mail and use errorMessage
}
}
private static bool TryGetResponse(HttpWebRequest request, out HttpWebResponse response, out string errorMessage)
{
errorMessage = null;
try
{
response = (HttpWebResponse)request.GetResponse();
// Everything ok, if we get here
return true;
}
catch (WebException e)
{
if (e.Response == null)
{
response = null;
if (e.Status == WebExceptionStatus.NameResolutionFailure)
{
errorMessage = "Name resolution failed.";
}
return false;
}
response = (HttpWebResponse)e.Response;
return true;
}
}
Upvotes: 2