I have built a dll, that return true if we have internet and false if not. We do the following: 1. call ping our server, if failed then try next 2. Call get http request to one of our WCF API, http://db.OurDomainName.com/WCFService.svc/GetCurrentTimeUnixFormat
This done in win service.
In PC and some of servers (windows server 2k R2) it works all the time for days no issue, each 2 min we wake up and test the connection.
However, in Azure virtual machine, it passed few times and then failed, WHY?
We tried, in web browser: http://db.OurDomainName.com/WCFService.svc/GetCurrentTimeUnixFormat and see the result, we click refresh for 2 min every sec, and see that it never failed.
But do the same from our WinService it failed after few times, start get timeout!!!!
We don't know why, seems like Firewall, but why it not block browser when refresh!!!
here what we done in .Net:
private static bool IsConnectedToInternetHttpRequest(string url = "http://db.OurDomainName.com/WCFService.svc/GetCurrentTimeUnixFormat")
{
//GET http://db.OurDomainName.com/WCFService.svc/GetCurrentTimeUnixFormat HTTP/1.1
//Host: db.tako.com
//Connection: keep-alive
//Cache-Control: max-age=0
//Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
//Accept-Encoding: gzip,deflate,sdch
//Accept-Language: en-US,en;q=0.8
try
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = (int)new TimeSpan(0, 0, 30).TotalMilliseconds;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36";
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Refresh);
request.KeepAlive = true;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
return true;
}
else
{
return false;
}
}
catch (Exception )
{
return false;
}
}