I am in a company network. In this network i can't ping external websites by IP. I can only call them by url like the browser does. Thats why i use WebRequest .
When i try to call "www.google.com" i got a "remote name could not be resolved (www.google.com)".
I have no "blocked" entry in my firewall. With this code i can call internal websites but not external.
This is my Code:
public void pingIP()
{
try
{
var url = uriBuilder;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.Uri);
//request.Accept = "*/*";
//request.UseDefaultCredentials = true;
//request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
//request.UserAgent = "Foo";
//request.Accept = "*/*";
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
_PingByURL = response.StatusCode == HttpStatusCode.OK;
}
catch
{
}
}
In app.config:
<system.net>
<defaultProxy enabled ="true" useDefaultCredentials = "true">
<proxy usesystemdefault ="True" bypassonlocal="True"/>
</defaultProxy>
</system.net>
How can i resolve the remote name? And is this the fast and cleanest way to check if a website is reachable by URL?
Thank you!
This is working without a problem !
static void Main(string[] args)
{
Console.WriteLine(PingTest());
}
public static bool PingTest()
{
Ping ping = new Ping();
PingReply pingStatus = ping.Send(IPAddress.Parse("208.69.34.231"));
//For the web address you need !
//PingReply pingStatus = ping.Send("www.google.com");
return pingStatus.Status == IPStatus.Success;
}
It is possible to have network restrictions and you are failing because of them.
this is what i use to ping which works for me
public bool ConnectedToInternet()
{
var myPing = new Ping();
const string host = "google.com";
var buffer = new byte[32];
const int timeout = 1000;
var pingOptions = new PingOptions();
var reply = myPing.Send(host, timeout, buffer, pingOptions);
return reply != null && reply.Status == IPStatus.Success;
}
try this and let me know if you get a reply or not
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.